Skip to content

Commit 3939f85

Browse files
committed
feat: add ts solution to lcci problem: No.17.11
No.17.11.Find Closest
1 parent 23149e0 commit 3939f85

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

lcci/17.11.Find Closest/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,27 @@
4040

4141
```
4242

43+
### **TypeScript**
44+
45+
```ts
46+
function findClosest(words: string[], word1: string, word2: string): number {
47+
let index1 = 100000;
48+
let index2 = -100000;
49+
let res = 100000;
50+
const n = words.length;
51+
for (let i = 0; i < n; i++) {
52+
const word = words[i];
53+
if (word === word1) {
54+
index1 = i;
55+
} else if (word === word2) {
56+
index2 = i;
57+
}
58+
res = Math.min(res, Math.abs(index1 - index2));
59+
}
60+
return res;
61+
}
62+
```
63+
4364
### **...**
4465

4566
```

lcci/17.11.Find Closest/README_EN.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@
3636

3737
```
3838

39+
### **TypeScript**
40+
41+
```ts
42+
function findClosest(words: string[], word1: string, word2: string): number {
43+
let index1 = 100000;
44+
let index2 = -100000;
45+
let res = 100000;
46+
const n = words.length;
47+
for (let i = 0; i < n; i++) {
48+
const word = words[i];
49+
if (word === word1) {
50+
index1 = i;
51+
} else if (word === word2) {
52+
index2 = i;
53+
}
54+
res = Math.min(res, Math.abs(index1 - index2));
55+
}
56+
return res;
57+
}
58+
```
59+
3960
### **...**
4061

4162
```

lcci/17.11.Find Closest/Solution.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function findClosest(words: string[], word1: string, word2: string): number {
2+
let index1 = 100000;
3+
let index2 = -100000;
4+
let res = 100000;
5+
const n = words.length;
6+
for (let i = 0; i < n; i++) {
7+
const word = words[i];
8+
if (word === word1) {
9+
index1 = i;
10+
} else if (word === word2) {
11+
index2 = i;
12+
}
13+
res = Math.min(res, Math.abs(index1 - index2));
14+
}
15+
return res;
16+
}

0 commit comments

Comments
 (0)