Skip to content

Commit 588bde3

Browse files
committed
feat: add solutions to lc problem: No.0016
No.0016.3Sum Closest
1 parent 9c2e5e7 commit 588bde3

File tree

8 files changed

+102
-20
lines changed

8 files changed

+102
-20
lines changed

solution/0000-0099/0010.Regular Expression Matching/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -311,28 +311,28 @@ var isMatch = function (s, p) {
311311
const f = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
312312
const dfs = (i, j) => {
313313
if (j >= n) {
314-
return i == m;
314+
return i === m;
315315
}
316316
if (f[i][j]) {
317-
return f[i][j] == 1;
317+
return f[i][j] === 1;
318318
}
319319
let res = -1;
320320
if (j + 1 < n && p[j + 1] === '*') {
321321
if (
322322
dfs(i, j + 2) ||
323-
(i < m && (s[i] == p[j] || p[j] == '.') && dfs(i + 1, j))
323+
(i < m && (s[i] === p[j] || p[j] === '.') && dfs(i + 1, j))
324324
) {
325325
res = 1;
326326
}
327327
} else if (
328328
i < m &&
329-
(s[i] == p[j] || p[j] == '.') &&
329+
(s[i] === p[j] || p[j] === '.') &&
330330
dfs(i + 1, j + 1)
331331
) {
332332
res = 1;
333333
}
334334
f[i][j] = res;
335-
return res == 1;
335+
return res === 1;
336336
};
337337
return dfs(0, 0);
338338
};
@@ -353,10 +353,10 @@ var isMatch = function (s, p) {
353353
for (let j = 1; j <= n; ++j) {
354354
if (p[j - 1] === '*') {
355355
f[i][j] = f[i][j - 2];
356-
if (i && (p[j - 2] === '.' || p[j - 2] == s[i - 1])) {
356+
if (i && (p[j - 2] === '.' || p[j - 2] === s[i - 1])) {
357357
f[i][j] |= f[i - 1][j];
358358
}
359-
} else if (i && (p[j - 1] === '.' || p[j - 1] == s[i - 1])) {
359+
} else if (i && (p[j - 1] === '.' || p[j - 1] === s[i - 1])) {
360360
f[i][j] = f[i - 1][j - 1];
361361
}
362362
}

solution/0000-0099/0010.Regular Expression Matching/README_EN.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -275,28 +275,28 @@ var isMatch = function (s, p) {
275275
const f = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
276276
const dfs = (i, j) => {
277277
if (j >= n) {
278-
return i == m;
278+
return i === m;
279279
}
280280
if (f[i][j]) {
281-
return f[i][j] == 1;
281+
return f[i][j] === 1;
282282
}
283283
let res = -1;
284284
if (j + 1 < n && p[j + 1] === '*') {
285285
if (
286286
dfs(i, j + 2) ||
287-
(i < m && (s[i] == p[j] || p[j] == '.') && dfs(i + 1, j))
287+
(i < m && (s[i] === p[j] || p[j] === '.') && dfs(i + 1, j))
288288
) {
289289
res = 1;
290290
}
291291
} else if (
292292
i < m &&
293-
(s[i] == p[j] || p[j] == '.') &&
293+
(s[i] === p[j] || p[j] === '.') &&
294294
dfs(i + 1, j + 1)
295295
) {
296296
res = 1;
297297
}
298298
f[i][j] = res;
299-
return res == 1;
299+
return res === 1;
300300
};
301301
return dfs(0, 0);
302302
};
@@ -317,10 +317,10 @@ var isMatch = function (s, p) {
317317
for (let j = 1; j <= n; ++j) {
318318
if (p[j - 1] === '*') {
319319
f[i][j] = f[i][j - 2];
320-
if (i && (p[j - 2] === '.' || p[j - 2] == s[i - 1])) {
320+
if (i && (p[j - 2] === '.' || p[j - 2] === s[i - 1])) {
321321
f[i][j] |= f[i - 1][j];
322322
}
323-
} else if (i && (p[j - 1] === '.' || p[j - 1] == s[i - 1])) {
323+
} else if (i && (p[j - 1] === '.' || p[j - 1] === s[i - 1])) {
324324
f[i][j] = f[i - 1][j - 1];
325325
}
326326
}

solution/0000-0099/0010.Regular Expression Matching/Solution.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ var isMatch = function (s, p) {
1212
for (let j = 1; j <= n; ++j) {
1313
if (p[j - 1] === '*') {
1414
f[i][j] = f[i][j - 2];
15-
if (i && (p[j - 2] === '.' || p[j - 2] == s[i - 1])) {
15+
if (i && (p[j - 2] === '.' || p[j - 2] === s[i - 1])) {
1616
f[i][j] |= f[i - 1][j];
1717
}
18-
} else if (i && (p[j - 1] === '.' || p[j - 1] == s[i - 1])) {
18+
} else if (i && (p[j - 1] === '.' || p[j - 1] === s[i - 1])) {
1919
f[i][j] = f[i - 1][j - 1];
2020
}
2121
}

solution/0000-0099/0016.3Sum Closest/README.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ var threeSumClosest = function (nums, target) {
184184
let k = n - 1;
185185
while (j < k) {
186186
const t = nums[i] + nums[j] + nums[k];
187-
if (t == target) {
187+
if (t === target) {
188188
return t;
189189
}
190190
if (Math.abs(t - target) < Math.abs(ans - target)) {
@@ -201,6 +201,35 @@ var threeSumClosest = function (nums, target) {
201201
};
202202
```
203203

204+
### **TypeScript**
205+
206+
```ts
207+
function threeSumClosest(nums: number[], target: number): number {
208+
nums.sort((a, b) => a - b);
209+
let ans: number = 1 << 30;
210+
const n = nums.length;
211+
for (let i = 0; i < n; ++i) {
212+
let j = i + 1;
213+
let k = n - 1;
214+
while (j < k) {
215+
const t: number = nums[i] + nums[j] + nums[k];
216+
if (t === target) {
217+
return t;
218+
}
219+
if (Math.abs(t - target) < Math.abs(ans - target)) {
220+
ans = t;
221+
}
222+
if (t > target) {
223+
--k;
224+
} else {
225+
++j;
226+
}
227+
}
228+
}
229+
return ans;
230+
}
231+
```
232+
204233
### **...**
205234

206235
```

solution/0000-0099/0016.3Sum Closest/README_EN.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ var threeSumClosest = function (nums, target) {
169169
let k = n - 1;
170170
while (j < k) {
171171
const t = nums[i] + nums[j] + nums[k];
172-
if (t == target) {
172+
if (t === target) {
173173
return t;
174174
}
175175
if (Math.abs(t - target) < Math.abs(ans - target)) {
@@ -186,6 +186,35 @@ var threeSumClosest = function (nums, target) {
186186
};
187187
```
188188

189+
### **TypeScript**
190+
191+
```ts
192+
function threeSumClosest(nums: number[], target: number): number {
193+
nums.sort((a, b) => a - b);
194+
let ans: number = 1 << 30;
195+
const n = nums.length;
196+
for (let i = 0; i < n; ++i) {
197+
let j = i + 1;
198+
let k = n - 1;
199+
while (j < k) {
200+
const t: number = nums[i] + nums[j] + nums[k];
201+
if (t === target) {
202+
return t;
203+
}
204+
if (Math.abs(t - target) < Math.abs(ans - target)) {
205+
ans = t;
206+
}
207+
if (t > target) {
208+
--k;
209+
} else {
210+
++j;
211+
}
212+
}
213+
}
214+
return ans;
215+
}
216+
```
217+
189218
### **...**
190219

191220
```

solution/0000-0099/0016.3Sum Closest/Solution.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var threeSumClosest = function (nums, target) {
1212
let k = n - 1;
1313
while (j < k) {
1414
const t = nums[i] + nums[j] + nums[k];
15-
if (t == target) {
15+
if (t === target) {
1616
return t;
1717
}
1818
if (Math.abs(t - target) < Math.abs(ans - target)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function threeSumClosest(nums: number[], target: number): number {
2+
nums.sort((a, b) => a - b);
3+
let ans: number = 1 << 30;
4+
const n = nums.length;
5+
for (let i = 0; i < n; ++i) {
6+
let j = i + 1;
7+
let k = n - 1;
8+
while (j < k) {
9+
const t: number = nums[i] + nums[j] + nums[k];
10+
if (t === target) {
11+
return t;
12+
}
13+
if (Math.abs(t - target) < Math.abs(ans - target)) {
14+
ans = t;
15+
}
16+
if (t > target) {
17+
--k;
18+
} else {
19+
++j;
20+
}
21+
}
22+
}
23+
return ans;
24+
}

solution/spider.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def get_question_detail(self, question_title_slug, retry_times=0):
188188
return question_detail
189189
except Exception as e:
190190
print(f'error:{str(e)}')
191-
time.sleep(random.choice(range(2, 5)))
191+
time.sleep(random.choice(range(2, 5)))
192192
return None
193193

194194
def handle(self, question: dict) -> dict:

0 commit comments

Comments
 (0)