Skip to content

Commit f02695d

Browse files
authoredMar 26, 2024
feat: add solutions to lc problems: No.170+ (doocs#2507)
1 parent d5441b5 commit f02695d

File tree

24 files changed

+403
-214
lines changed

24 files changed

+403
-214
lines changed
 

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

+37-13
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,18 @@ twoSum.find(7); // 没有两个整数加起来等于 7 ,返回 false</pre>
6868

6969
```python
7070
class TwoSum:
71+
7172
def __init__(self):
72-
self.cnt = Counter()
73+
self.cnt = defaultdict(int)
7374

7475
def add(self, number: int) -> None:
7576
self.cnt[number] += 1
7677

7778
def find(self, value: int) -> bool:
7879
for x, v in self.cnt.items():
7980
y = value - x
80-
if y in self.cnt:
81-
if x != y or v > 1:
82-
return True
81+
if y in self.cnt and (x != y or v > 1):
82+
return True
8383
return False
8484

8585

@@ -104,10 +104,8 @@ class TwoSum {
104104
for (var e : cnt.entrySet()) {
105105
int x = e.getKey(), v = e.getValue();
106106
int y = value - x;
107-
if (cnt.containsKey(y)) {
108-
if (x != y || v > 1) {
109-
return true;
110-
}
107+
if (cnt.containsKey(y) && (x != y || v > 1)) {
108+
return true;
111109
}
112110
}
113111
return false;
@@ -135,10 +133,8 @@ public:
135133
bool find(int value) {
136134
for (auto& [x, v] : cnt) {
137135
long y = (long) value - x;
138-
if (cnt.count(y)) {
139-
if (x != y || v > 1) {
140-
return true;
141-
}
136+
if (cnt.contains(y) && (x != y || v > 1)) {
137+
return true;
142138
}
143139
}
144140
return false;
@@ -166,7 +162,7 @@ func Constructor() TwoSum {
166162
}
167163
168164
func (this *TwoSum) Add(number int) {
169-
this.cnt[number]++
165+
this.cnt[number] += 1
170166
}
171167
172168
func (this *TwoSum) Find(value int) bool {
@@ -187,6 +183,34 @@ func (this *TwoSum) Find(value int) bool {
187183
*/
188184
```
189185

186+
```ts
187+
class TwoSum {
188+
private cnt: Map<number, number> = new Map();
189+
constructor() {}
190+
191+
add(number: number): void {
192+
this.cnt.set(number, (this.cnt.get(number) || 0) + 1);
193+
}
194+
195+
find(value: number): boolean {
196+
for (const [x, v] of this.cnt) {
197+
const y = value - x;
198+
if (this.cnt.has(y) && (x !== y || v > 1)) {
199+
return true;
200+
}
201+
}
202+
return false;
203+
}
204+
}
205+
206+
/**
207+
* Your TwoSum object will be instantiated and called as such:
208+
* var obj = new TwoSum()
209+
* obj.add(number)
210+
* var param_2 = obj.find(value)
211+
*/
212+
```
213+
190214
<!-- tabs:end -->
191215

192216
<!-- end -->

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

+51-14
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,37 @@ twoSum.find(7); // No two integers sum up to 7, return false
4646

4747
## Solutions
4848

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`.
5063

5164
<!-- tabs:start -->
5265

5366
```python
5467
class TwoSum:
68+
5569
def __init__(self):
56-
self.cnt = Counter()
70+
self.cnt = defaultdict(int)
5771

5872
def add(self, number: int) -> None:
5973
self.cnt[number] += 1
6074

6175
def find(self, value: int) -> bool:
6276
for x, v in self.cnt.items():
6377
y = value - x
64-
if y in self.cnt:
65-
if x != y or v > 1:
66-
return True
78+
if y in self.cnt and (x != y or v > 1):
79+
return True
6780
return False
6881

6982

@@ -88,10 +101,8 @@ class TwoSum {
88101
for (var e : cnt.entrySet()) {
89102
int x = e.getKey(), v = e.getValue();
90103
int y = value - x;
91-
if (cnt.containsKey(y)) {
92-
if (x != y || v > 1) {
93-
return true;
94-
}
104+
if (cnt.containsKey(y) && (x != y || v > 1)) {
105+
return true;
95106
}
96107
}
97108
return false;
@@ -119,10 +130,8 @@ public:
119130
bool find(int value) {
120131
for (auto& [x, v] : cnt) {
121132
long y = (long) value - x;
122-
if (cnt.count(y)) {
123-
if (x != y || v > 1) {
124-
return true;
125-
}
133+
if (cnt.contains(y) && (x != y || v > 1)) {
134+
return true;
126135
}
127136
}
128137
return false;
@@ -150,7 +159,7 @@ func Constructor() TwoSum {
150159
}
151160
152161
func (this *TwoSum) Add(number int) {
153-
this.cnt[number]++
162+
this.cnt[number] += 1
154163
}
155164
156165
func (this *TwoSum) Find(value int) bool {
@@ -171,6 +180,34 @@ func (this *TwoSum) Find(value int) bool {
171180
*/
172181
```
173182

183+
```ts
184+
class TwoSum {
185+
private cnt: Map<number, number> = new Map();
186+
constructor() {}
187+
188+
add(number: number): void {
189+
this.cnt.set(number, (this.cnt.get(number) || 0) + 1);
190+
}
191+
192+
find(value: number): boolean {
193+
for (const [x, v] of this.cnt) {
194+
const y = value - x;
195+
if (this.cnt.has(y) && (x !== y || v > 1)) {
196+
return true;
197+
}
198+
}
199+
return false;
200+
}
201+
}
202+
203+
/**
204+
* Your TwoSum object will be instantiated and called as such:
205+
* var obj = new TwoSum()
206+
* obj.add(number)
207+
* var param_2 = obj.find(value)
208+
*/
209+
```
210+
174211
<!-- tabs:end -->
175212

176213
<!-- end -->

‎solution/0100-0199/0170.Two Sum III - Data structure design/Solution.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ class TwoSum {
1010
bool find(int value) {
1111
for (auto& [x, v] : cnt) {
1212
long y = (long) value - x;
13-
if (cnt.count(y)) {
14-
if (x != y || v > 1) {
15-
return true;
16-
}
13+
if (cnt.contains(y) && (x != y || v > 1)) {
14+
return true;
1715
}
1816
}
1917
return false;

‎solution/0100-0199/0170.Two Sum III - Data structure design/Solution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func Constructor() TwoSum {
77
}
88

99
func (this *TwoSum) Add(number int) {
10-
this.cnt[number]++
10+
this.cnt[number] += 1
1111
}
1212

1313
func (this *TwoSum) Find(value int) bool {

‎solution/0100-0199/0170.Two Sum III - Data structure design/Solution.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ public boolean find(int value) {
1212
for (var e : cnt.entrySet()) {
1313
int x = e.getKey(), v = e.getValue();
1414
int y = value - x;
15-
if (cnt.containsKey(y)) {
16-
if (x != y || v > 1) {
17-
return true;
18-
}
15+
if (cnt.containsKey(y) && (x != y || v > 1)) {
16+
return true;
1917
}
2018
}
2119
return false;

‎solution/0100-0199/0170.Two Sum III - Data structure design/Solution.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
class TwoSum:
2+
23
def __init__(self):
3-
self.cnt = Counter()
4+
self.cnt = defaultdict(int)
45

56
def add(self, number: int) -> None:
67
self.cnt[number] += 1
78

89
def find(self, value: int) -> bool:
910
for x, v in self.cnt.items():
1011
y = value - x
11-
if y in self.cnt:
12-
if x != y or v > 1:
13-
return True
12+
if y in self.cnt and (x != y or v > 1):
13+
return True
1414
return False
1515

1616

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class TwoSum {
2+
private cnt: Map<number, number> = new Map();
3+
constructor() {}
4+
5+
add(number: number): void {
6+
this.cnt.set(number, (this.cnt.get(number) || 0) + 1);
7+
}
8+
9+
find(value: number): boolean {
10+
for (const [x, v] of this.cnt) {
11+
const y = value - x;
12+
if (this.cnt.has(y) && (x !== y || v > 1)) {
13+
return true;
14+
}
15+
}
16+
return false;
17+
}
18+
}
19+
20+
/**
21+
* Your TwoSum object will be instantiated and called as such:
22+
* var obj = new TwoSum()
23+
* obj.add(number)
24+
* var param_2 = obj.find(value)
25+
*/

‎solution/0100-0199/0171.Excel Sheet Column Number/README.md

+38-21
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,33 @@ AB -&gt; 28
5656

5757
## 解法
5858

59-
### 方法一
59+
### 方法一:进制转换
60+
61+
Excel 表格中的列名称是一种 26 进制的表示方法。例如,"AB" 表示的列序号是 $1 \times 26 + 2 = 28$。
62+
63+
因此,我们可以遍历字符串 `columnTitle`,将每个字符转换为对应的数值,然后计算结果即可。
64+
65+
时间复杂度 $O(n)$,其中 $n$ 是字符串 `columnTitle` 的长度。空间复杂度 $O(1)$。
6066

6167
<!-- tabs:start -->
6268

6369
```python
6470
class Solution:
6571
def titleToNumber(self, columnTitle: str) -> int:
66-
res = 0
67-
for c in columnTitle:
68-
res = res * 26 + (ord(c) - ord('A') + 1)
69-
return res
72+
ans = 0
73+
for c in map(ord, columnTitle):
74+
ans = ans * 26 + c - ord("A") + 1
75+
return ans
7076
```
7177

7278
```java
7379
class Solution {
7480
public int titleToNumber(String columnTitle) {
75-
int res = 0;
76-
for (char c : columnTitle.toCharArray()) {
77-
res = res * 26 + (c - 'A' + 1);
81+
int ans = 0;
82+
for (int i = 0; i < columnTitle.length(); ++i) {
83+
ans = ans * 26 + (columnTitle.charAt(i) - 'A' + 1);
7884
}
79-
return res;
85+
return ans;
8086
}
8187
}
8288
```
@@ -85,32 +91,43 @@ class Solution {
8591
class Solution {
8692
public:
8793
int titleToNumber(string columnTitle) {
88-
int res = 0;
89-
for (char c : columnTitle) {
90-
res = res * 26 + (c - 'A' + 1);
94+
int ans = 0;
95+
for (char& c : columnTitle) {
96+
ans = ans * 26 + (c - 'A' + 1);
9197
}
92-
return res;
98+
return ans;
9399
}
94100
};
95101
```
96102
97103
```go
98-
func titleToNumber(columnTitle string) int {
99-
res := 0
104+
func titleToNumber(columnTitle string) (ans int) {
100105
for _, c := range columnTitle {
101-
res = res*26 + int(c-'A'+1)
106+
ans = ans*26 + int(c-'A'+1)
102107
}
103-
return res
108+
return
104109
}
105110
```
106111

107112
```ts
108113
function titleToNumber(columnTitle: string): number {
109-
let res: number = 0;
110-
for (let char of columnTitle) {
111-
res = res * 26 + char.charCodeAt(0) - 64;
114+
let ans: number = 0;
115+
for (const c of columnTitle) {
116+
ans = ans * 26 + (c.charCodeAt(0) - 'A'.charCodeAt(0) + 1);
117+
}
118+
return ans;
119+
}
120+
```
121+
122+
```cs
123+
public class Solution {
124+
public int TitleToNumber(string columnTitle) {
125+
int ans = 0;
126+
foreach (char c in columnTitle) {
127+
ans = ans * 26 + c - 'A' + 1;
128+
}
129+
return ans;
112130
}
113-
return res;
114131
}
115132
```
116133

0 commit comments

Comments
 (0)
Please sign in to comment.