Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3e3d06c

Browse files
authored
feat: add solutions to lc problem: No.0245 (doocs#4433)
1 parent ec22c87 commit 3e3d06c

File tree

3 files changed

+109
-6
lines changed

3 files changed

+109
-6
lines changed

solution/0200-0299/0245.Shortest Word Distance III/README.md

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,12 @@ tags:
5656

5757
### 方法一:分情况讨论
5858

59-
先判断 `word1``word2` 是否相等:
59+
我们首先判断 $\textit{word1}$$\textit{word2}$ 是否相等:
6060

61-
如果相等,遍历数组 `wordsDict`,找到两个 `word1` 的下标 $i$ 和 $j$,求 $i-j$ 的最小值。
61+
- 如果相等,遍历数组 $\textit{wordsDict}$,找到两个 $\textit{word1}$ 的下标 $i$ 和 $j$,求 $i-j$ 的最小值。
62+
- 如果不相等,遍历数组 $\textit{wordsDict}$,找到 $\textit{word1}$ 和 $\textit{word2}$ 的下标 $i$ 和 $j$,求 $i-j$ 的最小值。
6263

63-
如果不相等,遍历数组 `wordsDict`,找到 `word1``word2` 的下标 $i$ 和 $j$,求 $i-j$ 的最小值。
64-
65-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `wordsDict` 的长度。
64+
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{wordsDict}$ 的长度。空间复杂度 $O(1)$。
6665

6766
<!-- tabs:start -->
6867

@@ -199,6 +198,40 @@ func abs(x int) int {
199198
}
200199
```
201200

201+
#### TypeScript
202+
203+
```ts
204+
function shortestWordDistance(wordsDict: string[], word1: string, word2: string): number {
205+
let ans = wordsDict.length;
206+
if (word1 === word2) {
207+
let j = -1;
208+
for (let i = 0; i < wordsDict.length; i++) {
209+
if (wordsDict[i] === word1) {
210+
if (j !== -1) {
211+
ans = Math.min(ans, i - j);
212+
}
213+
j = i;
214+
}
215+
}
216+
} else {
217+
let i = -1,
218+
j = -1;
219+
for (let k = 0; k < wordsDict.length; k++) {
220+
if (wordsDict[k] === word1) {
221+
i = k;
222+
}
223+
if (wordsDict[k] === word2) {
224+
j = k;
225+
}
226+
if (i !== -1 && j !== -1) {
227+
ans = Math.min(ans, Math.abs(i - j));
228+
}
229+
}
230+
}
231+
return ans;
232+
}
233+
```
234+
202235
<!-- tabs:end -->
203236

204237
<!-- solution:end -->

solution/0200-0299/0245.Shortest Word Distance III/README_EN.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ tags:
4545

4646
<!-- solution:start -->
4747

48-
### Solution 1
48+
### Solution 1: Case Analysis
49+
50+
First, we check whether $\textit{word1}$ and $\textit{word2}$ are equal:
51+
52+
- If they are equal, iterate through the array $\textit{wordsDict}$ to find two indices $i$ and $j$ of $\textit{word1}$, and compute the minimum value of $i-j$.
53+
- If they are not equal, iterate through the array $\textit{wordsDict}$ to find the indices $i$ of $\textit{word1}$ and $j$ of $\textit{word2}$, and compute the minimum value of $i-j$.
54+
55+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{wordsDict}$. The space complexity is $O(1)$.
4956

5057
<!-- tabs:start -->
5158

@@ -182,6 +189,40 @@ func abs(x int) int {
182189
}
183190
```
184191

192+
#### TypeScript
193+
194+
```ts
195+
function shortestWordDistance(wordsDict: string[], word1: string, word2: string): number {
196+
let ans = wordsDict.length;
197+
if (word1 === word2) {
198+
let j = -1;
199+
for (let i = 0; i < wordsDict.length; i++) {
200+
if (wordsDict[i] === word1) {
201+
if (j !== -1) {
202+
ans = Math.min(ans, i - j);
203+
}
204+
j = i;
205+
}
206+
}
207+
} else {
208+
let i = -1,
209+
j = -1;
210+
for (let k = 0; k < wordsDict.length; k++) {
211+
if (wordsDict[k] === word1) {
212+
i = k;
213+
}
214+
if (wordsDict[k] === word2) {
215+
j = k;
216+
}
217+
if (i !== -1 && j !== -1) {
218+
ans = Math.min(ans, Math.abs(i - j));
219+
}
220+
}
221+
}
222+
return ans;
223+
}
224+
```
225+
185226
<!-- tabs:end -->
186227

187228
<!-- solution:end -->
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function shortestWordDistance(wordsDict: string[], word1: string, word2: string): number {
2+
let ans = wordsDict.length;
3+
if (word1 === word2) {
4+
let j = -1;
5+
for (let i = 0; i < wordsDict.length; i++) {
6+
if (wordsDict[i] === word1) {
7+
if (j !== -1) {
8+
ans = Math.min(ans, i - j);
9+
}
10+
j = i;
11+
}
12+
}
13+
} else {
14+
let i = -1,
15+
j = -1;
16+
for (let k = 0; k < wordsDict.length; k++) {
17+
if (wordsDict[k] === word1) {
18+
i = k;
19+
}
20+
if (wordsDict[k] === word2) {
21+
j = k;
22+
}
23+
if (i !== -1 && j !== -1) {
24+
ans = Math.min(ans, Math.abs(i - j));
25+
}
26+
}
27+
}
28+
return ans;
29+
}

0 commit comments

Comments
 (0)