You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0100-0199/0170.Two Sum III - Data structure design/README_EN.md
+51-14
Original file line number
Diff line number
Diff line change
@@ -46,24 +46,37 @@ twoSum.find(7); // No two integers sum up to 7, return false
46
46
47
47
## Solutions
48
48
49
-
### Solution 1
49
+
### Solution 1: Hash Table
50
+
51
+
We use a hash table `cnt` to store the count of each number.
52
+
53
+
When the `add` method is called, we increment the count of the number `number`.
54
+
55
+
When the `find` method is called, we iterate over the hash table `cnt`. For each key `x`, we check if `value - x` is also a key in the hash table `cnt`. If it is, we check if `x` is equal to `value - x`. If they are not equal, it means we have found a pair of numbers whose sum is `value`, and we return `true`. If they are equal, we check if the count of `x` is greater than `1`. If it is, it means we have found a pair of numbers whose sum is `value`, and we return `true`. If it is less than or equal to `1`, it means we have not found a pair of numbers whose sum is `value`, and we continue to iterate over the hash table `cnt`. If we have not found a pair after the iteration, we return `false`.
56
+
57
+
Time complexity:
58
+
59
+
- The time complexity of the `add` method is $O(1)$.
60
+
- The time complexity of the `find` method is $O(n)$.
61
+
62
+
Space complexity is $O(n)$, where $n$ is the size of the hash table `cnt`.
0 commit comments