We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7d3641c commit 865575dCopy full SHA for 865575d
0170-two-sum-iii-data-structure-design/0170-two-sum-iii-data-structure-design.py
@@ -0,0 +1,27 @@
1
+# time complexity: O(n)
2
+# space complexity: O(n)
3
+class TwoSum:
4
+
5
+ def __init__(self):
6
+ self.nums = {}
7
8
+ def add(self, number: int) -> None:
9
+ if number in self.nums:
10
+ self.nums[number] += 1
11
+ else:
12
+ self.nums[number] = 1
13
14
+ def find(self, value: int) -> bool:
15
+ for num in self.nums:
16
+ remain = value - num
17
+ if remain in self.nums:
18
+ if remain != num or self.nums[num] > 1:
19
+ return True
20
+ return False
21
22
23
+twoSum = TwoSum()
24
+twoSum.add(1)
25
+twoSum.add(-1)
26
+print(twoSum.find(0))
27
+print(twoSum.find(7))
0 commit comments