[LintCode] Two Sum - Data Structure Design

Design and implement a TwoSum class. It should support the following operations: add and find.

add - Add the number to an internal data structure.
find - Find if there exists any pair of numbers which sum is equal to the value.

Example

add(1); add(3); add(5);

find(4) // return true
find(7) // return false

For two sum, typically we have two solutions to select from: using hash table or sorting then using two pointers technique.Using hash table gives us a O(n) runtime and O(n) space algorithm; Using sorting/two pointers technique gives us a O(n*logn) time, and O(1) space(if quick sort is used) algorithm. 

This problem is different with the Two Sum problem in the following two aspects.1. We need to design a data structure that supports adding new element to the internal data structure that stores all elements. 2. We only check if such a pair exists or not; no need to return the indices as is required in the Two Sum problem. 

Since we need to maintain such an internal data structure that stores all elements and an add element operation, we need to use O(n) space for this regardless which approachwe use. So this makes the hash table solution better as it has a better runtime of O(n).

Solution. O(n) time, O(n) space, Hash tableSince we only check if such a pair exists, a hash set and one pass is sufficient. Another implementation is to iterate through the list and store all key-value pairs(value - nums[i], nums[i])in a hash map. Then during the second pass, for each element nums[i], check if a key of value - nums[i] exists. This still gives us O(n) runtime but obviously not as goodas the one pass solution. However, this two passes solution is useful when we needto return indices as is required in Two Sum.

 1 public class TwoSum {
 2     private ArrayList<Integer> numbers;
 3
 4     public TwoSum()
 5     {
 6         this.numbers = new ArrayList<Integer>();
 7     }
 8
 9     // Add the number to an internal data structure.
10     public void add(int number) {
11         this.numbers.add(number);
12     }
13
14     // Find if there exists any pair of numbers which sum is equal to the value.
15     public boolean find(int value) {
16         HashSet<Integer> set = new HashSet<Integer>();
17         for(int i = 0; i < this.numbers.size(); i++)
18         {
19             if(set.contains(numbers.get(i)))
20             {
21                 return true;
22             }
23             set.add(value - numbers.get(i));
24         }
25         return false;
26     }
27 }
28 // Your TwoSum object will be instantiated and called as such:
29 // TwoSum twoSum = new TwoSum();
30 // twoSum.add(number);
31 // twoSum.find(value);


Related ProblemsTwo Sum - Input array is sorted Word Abbreviation SetTwo Sum - Difference equals to target Two Sum - Less than or equal to target Two Sum - Unique pairs Two Sum - Closest to target Two Sum - Greater than targetTwo Sum 
时间: 2024-11-07 11:40:47

[LintCode] Two Sum - Data Structure Design的相关文章

leetcode 170: Two Sum III - Data structure design

Two Sum III - Data structure design Total Accepted: 311 Total Submissions: 1345 Design and implement a TwoSum class. It should support the following operations:add and find. add - Add the number to an internal data structure. find - Find if there exi

[LeetCode][JavaScript]Add and Search Word - Data structure design

Add and Search Word - Data structure design Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or 

【LeetCode】211. Add and Search Word - Data structure design

Add and Search Word - Data structure design Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or 

170. Two Sum III - Data structure design

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value. For example, add(1); ad

LeetCode-Two Sum III - Data structure design

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure. find - Find if there exists any pair of numbers which sum is equal to the value. For example, add(1); a

Two Sum III - Data structure design

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value. For example, add(1); ad

Two Sum III - Data structure design(leetcode170)

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value. For example, add(1); ad

leetcode3 Two Sum III – Data structure design

Question: Design and implement a TwoSum class. It should support the following operations: add and find. add(input) – Add the number input to an internal data structure. find(value) – Find if there exists any pair of numbers which sum is equal to the

[LC] 170. Two Sum III - Data structure design

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value. Example 1: add(1); add(