-
Notifications
You must be signed in to change notification settings - Fork 13
/
170.两数之和 III - 数据结构设计.py
48 lines (41 loc) · 1.29 KB
/
170.两数之和 III - 数据结构设计.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 设计并实现一个 TwoSum 的类,使该类需要支持 add 和 find 的操作。
#
# add 操作 - 对内部数据结构增加一个数。
# find 操作 - 寻找内部数据结构中是否存在一对整数,使得两数之和与给定的数相等。
#
# 示例 1:
#
# add(1); add(3); add(5);
# find(4) -> true
# find(7) -> false
# 示例 2:
#
# add(3); add(1); add(2);
# find(3) -> true
# find(6) -> false
class TwoSum:
def __init__(self):
"""
Initialize your data structure here.
"""
self.data = {}
def add(self, number: int) -> None:
"""
Add the number to an internal data structure..
"""
self.data[number] = self.data.get(number, 0) + 1
def find(self, value: int) -> bool:
"""
Find if there exists any pair of numbers which sum is equal to the value.
"""
for num in self.data.keys():
if value - num in self.data:
if value == 2 * num and self.data.get(num) >= 2:
return True
elif value - num != num:
return True
return False
# Your TwoSum object will be instantiated and called as such:
# obj = TwoSum()
# obj.add(number)
# param_2 = obj.find(value)