Skip to content

Commit 865575d

Browse files
committed
Time: 145 ms (88.57%), Space: 23.1 MB (38.14%) - LeetHub
1 parent 7d3641c commit 865575d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)