Skip to content

Commit a26365c

Browse files
committed
feat: add solutions to lc problem: No.0170
No.0170.Two Sum III - Data structure design
1 parent 3348f0e commit a26365c

File tree

6 files changed

+260
-95
lines changed

6 files changed

+260
-95
lines changed

solution/0100-0199/0170.Two Sum III - Data structure design/README.md

+98-31
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,20 @@ twoSum.find(7); // 没有两个整数加起来等于 7 ,返回 false</pre>
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52-
“计数器”实现。
52+
**方法一:哈希表**
53+
54+
我们用哈希表 `cnt` 存储数字出现的次数。
55+
56+
调用 `add` 方法时,将数字 `number` 的出现次数加一。
57+
58+
调用 `find` 方法时,遍历哈希表 `cnt`,对于每个键 `x`,判断 `value - x` 是否也是哈希表 `cnt` 的键,如果是,判断 `x` 是否等于 `value - x`,如果不等,说明找到了一对和为 `value` 的数字,返回 `true`;如果等,判断 `x` 的出现次数是否大于 `1`,如果大于 `1`,说明找到了一对和为 `value` 的数字,返回 `true`;如果小于等于 `1`,说明没有找到一对和为 `value` 的数字,继续遍历哈希表 `cnt`,如果遍历结束都没有找到,返回 `false`
59+
60+
时间复杂度:
61+
62+
- `add` 方法的时间复杂度为 $O(1)$;
63+
- `find` 方法的时间复杂度为 $O(n)$。
64+
65+
空间复杂度 $O(n)$,其中 $n$ 为哈希表 `cnt` 的大小。
5366

5467
<!-- tabs:start -->
5568

@@ -60,27 +73,16 @@ twoSum.find(7); // 没有两个整数加起来等于 7 ,返回 false</pre>
6073
```python
6174
class TwoSum:
6275
def __init__(self):
63-
"""
64-
Initialize your data structure here.
65-
"""
66-
self.counter = Counter()
76+
self.cnt = Counter()
6777

6878
def add(self, number: int) -> None:
69-
"""
70-
Add the number to an internal data structure..
71-
"""
72-
self.counter[number] += 1
79+
self.cnt[number] += 1
7380

7481
def find(self, value: int) -> bool:
75-
"""
76-
Find if there exists any pair of numbers which sum is equal to the value.
77-
"""
78-
for num in self.counter.keys():
79-
other = value - num
80-
if other in self.counter:
81-
if other != num:
82-
return True
83-
if other == num and self.counter[num] > 1:
82+
for x, v in self.cnt.items():
83+
y = value - x
84+
if y in self.cnt:
85+
if x != y or v > 1:
8486
return True
8587
return False
8688

@@ -97,27 +99,22 @@ class TwoSum:
9799

98100
```java
99101
class TwoSum {
100-
private Map<Integer, Integer> counter;
102+
private Map<Integer, Integer> cnt = new HashMap<>();
101103

102-
/** Initialize your data structure here. */
103104
public TwoSum() {
104-
counter = new HashMap<>();
105+
105106
}
106107

107-
/** Add the number to an internal data structure.. */
108108
public void add(int number) {
109-
counter.put(number, counter.getOrDefault(number, 0) + 1);
109+
cnt.merge(number, 1, Integer::sum);
110110
}
111111

112-
/** Find if there exists any pair of numbers which sum is equal to the value. */
113112
public boolean find(int value) {
114-
for (int num : counter.keySet()) {
115-
int other = value - num;
116-
if (counter.containsKey(other)) {
117-
if (num != other) {
118-
return true;
119-
}
120-
if (num == other && counter.get(other) > 1) {
113+
for (var e : cnt.entrySet()) {
114+
int x = e.getKey(), v = e.getValue();
115+
int y = value - x;
116+
if (cnt.containsKey(y)) {
117+
if (x != y || v > 1) {
121118
return true;
122119
}
123120
}
@@ -134,6 +131,76 @@ class TwoSum {
134131
*/
135132
```
136133

134+
### **C++**
135+
136+
```cpp
137+
class TwoSum {
138+
public:
139+
TwoSum() {
140+
141+
}
142+
143+
void add(int number) {
144+
++cnt[number];
145+
}
146+
147+
bool find(int value) {
148+
for (auto& [x, v] : cnt) {
149+
long y = (long) value - x;
150+
if (cnt.count(y)) {
151+
if (x != y || v > 1) {
152+
return true;
153+
}
154+
}
155+
}
156+
return false;
157+
}
158+
159+
private:
160+
unordered_map<int, int> cnt;
161+
};
162+
163+
/**
164+
* Your TwoSum object will be instantiated and called as such:
165+
* TwoSum* obj = new TwoSum();
166+
* obj->add(number);
167+
* bool param_2 = obj->find(value);
168+
*/
169+
```
170+
171+
### **Go**
172+
173+
```go
174+
type TwoSum struct {
175+
cnt map[int]int
176+
}
177+
178+
func Constructor() TwoSum {
179+
return TwoSum{map[int]int{}}
180+
}
181+
182+
func (this *TwoSum) Add(number int) {
183+
this.cnt[number]++
184+
}
185+
186+
func (this *TwoSum) Find(value int) bool {
187+
for x, v := range this.cnt {
188+
y := value - x
189+
if _, ok := this.cnt[y]; ok && (x != y || v > 1) {
190+
return true
191+
}
192+
}
193+
return false
194+
}
195+
196+
/**
197+
* Your TwoSum object will be instantiated and called as such:
198+
* obj := Constructor();
199+
* obj.Add(number);
200+
* param_2 := obj.Find(value);
201+
*/
202+
```
203+
137204
### **...**
138205

139206
```

solution/0100-0199/0170.Two Sum III - Data structure design/README_EN.md

+86-32
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,16 @@ twoSum.find(7); // No two integers sum up to 7, return false
5151
```python
5252
class TwoSum:
5353
def __init__(self):
54-
"""
55-
Initialize your data structure here.
56-
"""
57-
self.counter = Counter()
54+
self.cnt = Counter()
5855

5956
def add(self, number: int) -> None:
60-
"""
61-
Add the number to an internal data structure..
62-
"""
63-
self.counter[number] += 1
57+
self.cnt[number] += 1
6458

6559
def find(self, value: int) -> bool:
66-
"""
67-
Find if there exists any pair of numbers which sum is equal to the value.
68-
"""
69-
for num in self.counter.keys():
70-
other = value - num
71-
if other in self.counter:
72-
if other != num:
73-
return True
74-
if other == num and self.counter[num] > 1:
60+
for x, v in self.cnt.items():
61+
y = value - x
62+
if y in self.cnt:
63+
if x != y or v > 1:
7564
return True
7665
return False
7766

@@ -86,27 +75,22 @@ class TwoSum:
8675

8776
```java
8877
class TwoSum {
89-
private Map<Integer, Integer> counter;
78+
private Map<Integer, Integer> cnt = new HashMap<>();
9079

91-
/** Initialize your data structure here. */
9280
public TwoSum() {
93-
counter = new HashMap<>();
94-
}
9581

96-
/** Add the number to an internal data structure.. */
82+
}
83+
9784
public void add(int number) {
98-
counter.put(number, counter.getOrDefault(number, 0) + 1);
85+
cnt.merge(number, 1, Integer::sum);
9986
}
100-
101-
/** Find if there exists any pair of numbers which sum is equal to the value. */
87+
10288
public boolean find(int value) {
103-
for (int num : counter.keySet()) {
104-
int other = value - num;
105-
if (counter.containsKey(other)) {
106-
if (num != other) {
107-
return true;
108-
}
109-
if (num == other && counter.get(other) > 1) {
89+
for (var e : cnt.entrySet()) {
90+
int x = e.getKey(), v = e.getValue();
91+
int y = value - x;
92+
if (cnt.containsKey(y)) {
93+
if (x != y || v > 1) {
11094
return true;
11195
}
11296
}
@@ -123,6 +107,76 @@ class TwoSum {
123107
*/
124108
```
125109

110+
### **C++**
111+
112+
```cpp
113+
class TwoSum {
114+
public:
115+
TwoSum() {
116+
117+
}
118+
119+
void add(int number) {
120+
++cnt[number];
121+
}
122+
123+
bool find(int value) {
124+
for (auto& [x, v] : cnt) {
125+
long y = (long) value - x;
126+
if (cnt.count(y)) {
127+
if (x != y || v > 1) {
128+
return true;
129+
}
130+
}
131+
}
132+
return false;
133+
}
134+
135+
private:
136+
unordered_map<int, int> cnt;
137+
};
138+
139+
/**
140+
* Your TwoSum object will be instantiated and called as such:
141+
* TwoSum* obj = new TwoSum();
142+
* obj->add(number);
143+
* bool param_2 = obj->find(value);
144+
*/
145+
```
146+
147+
### **Go**
148+
149+
```go
150+
type TwoSum struct {
151+
cnt map[int]int
152+
}
153+
154+
func Constructor() TwoSum {
155+
return TwoSum{map[int]int{}}
156+
}
157+
158+
func (this *TwoSum) Add(number int) {
159+
this.cnt[number]++
160+
}
161+
162+
func (this *TwoSum) Find(value int) bool {
163+
for x, v := range this.cnt {
164+
y := value - x
165+
if _, ok := this.cnt[y]; ok && (x != y || v > 1) {
166+
return true
167+
}
168+
}
169+
return false
170+
}
171+
172+
/**
173+
* Your TwoSum object will be instantiated and called as such:
174+
* obj := Constructor();
175+
* obj.Add(number);
176+
* param_2 := obj.Find(value);
177+
*/
178+
```
179+
126180
### **...**
127181

128182
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class TwoSum {
2+
public:
3+
TwoSum() {
4+
5+
}
6+
7+
void add(int number) {
8+
++cnt[number];
9+
}
10+
11+
bool find(int value) {
12+
for (auto& [x, v] : cnt) {
13+
long y = (long) value - x;
14+
if (cnt.count(y)) {
15+
if (x != y || v > 1) {
16+
return true;
17+
}
18+
}
19+
}
20+
return false;
21+
}
22+
23+
private:
24+
unordered_map<int, int> cnt;
25+
};
26+
27+
/**
28+
* Your TwoSum object will be instantiated and called as such:
29+
* TwoSum* obj = new TwoSum();
30+
* obj->add(number);
31+
* bool param_2 = obj->find(value);
32+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
type TwoSum struct {
2+
cnt map[int]int
3+
}
4+
5+
func Constructor() TwoSum {
6+
return TwoSum{map[int]int{}}
7+
}
8+
9+
func (this *TwoSum) Add(number int) {
10+
this.cnt[number]++
11+
}
12+
13+
func (this *TwoSum) Find(value int) bool {
14+
for x, v := range this.cnt {
15+
y := value - x
16+
if _, ok := this.cnt[y]; ok && (x != y || v > 1) {
17+
return true
18+
}
19+
}
20+
return false
21+
}
22+
23+
/**
24+
* Your TwoSum object will be instantiated and called as such:
25+
* obj := Constructor();
26+
* obj.Add(number);
27+
* param_2 := obj.Find(value);
28+
*/

0 commit comments

Comments
 (0)