Skip to content

Commit 42963f4

Browse files
committed
feat: add solutions to lc problems: No.0243~0245
* No.0243.Shortest Word Distance * No.0244.Shortest Word Distance II * No.0245.Shortest Word Distance III
1 parent c38214d commit 42963f4

File tree

21 files changed

+924
-265
lines changed

21 files changed

+924
-265
lines changed

solution/0200-0299/0243.Shortest Word Distance/README.md

+77-29
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42-
用两个指针 `i1`, `i2` 保存 `word1``word2` 最近出现的位置,然后每次计算距离 `Math.abs(i1 - i2)` 是否比此前的记录更小,是则更新最短距离。
42+
**方法一:双指针**
43+
44+
遍历数组 `wordsDict`,找到 `word1``word2` 的下标 $i$ 和 $j$,求 $i-j$ 的最小值。
45+
46+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `wordsDict` 的长度。
4347

4448
<!-- tabs:start -->
4549

@@ -50,16 +54,16 @@
5054
```python
5155
class Solution:
5256
def shortestDistance(self, wordsDict: List[str], word1: str, word2: str) -> int:
53-
i1 = i2 = -1
54-
shortest_distance = len(wordsDict)
55-
for i in range(len(wordsDict)):
56-
if wordsDict[i] == word1:
57-
i1 = i
58-
elif wordsDict[i] == word2:
59-
i2 = i
60-
if i1 != -1 and i2 != -1:
61-
shortest_distance = min(shortest_distance, abs(i1 - i2))
62-
return shortest_distance
57+
i = j = -1
58+
ans = inf
59+
for k, w in enumerate(wordsDict):
60+
if w == word1:
61+
i = k
62+
if w == word2:
63+
j = k
64+
if i != -1 and j != -1:
65+
ans = min(ans, abs(i - j))
66+
return ans
6367
```
6468

6569
### **Java**
@@ -69,34 +73,78 @@ class Solution:
6973
```java
7074
class Solution {
7175
public int shortestDistance(String[] wordsDict, String word1, String word2) {
72-
int i1 = -1, i2 = -1;
73-
int shortestDistance = wordsDict.length;
74-
for (int i = 0; i < wordsDict.length; ++i) {
75-
if (word1.equals(wordsDict[i])) {
76-
i1 = i;
77-
} else if (word2.equals(wordsDict[i])) {
78-
i2 = i;
76+
int ans = 0x3f3f3f3f;
77+
for (int k = 0, i = -1, j = -1; k < wordsDict.length; ++k) {
78+
if (wordsDict[k].equals(word1)) {
79+
i = k;
7980
}
80-
if (i1 != -1 && i2 != -1) {
81-
shortestDistance = Math.min(shortestDistance, Math.abs(i1 - i2));
81+
if (wordsDict[k].equals(word2)) {
82+
j = k;
83+
}
84+
if (i != -1 && j != -1) {
85+
ans = Math.min(ans, Math.abs(i - j));
8286
}
8387
}
84-
return shortestDistance;
88+
return ans;
8589
}
8690
}
8791
```
8892

89-
### **TypeScript**
93+
### **C++**
9094

91-
```ts
92-
function integerBreak(n: number): number {
93-
let dp = new Array(n + 1).fill(1);
94-
for (let i = 3; i <= n; i++) {
95-
for (let j = 1; j < i; j++) {
96-
dp[i] = Math.max(dp[i], j * (i - j), j * dp[i - j]);
95+
```cpp
96+
class Solution {
97+
public:
98+
int shortestDistance(vector<string>& wordsDict, string word1, string word2) {
99+
int ans = INT_MAX;
100+
for (int k = 0, i = -1, j = -1; k < wordsDict.size(); ++k) {
101+
if (wordsDict[k] == word1) {
102+
i = k;
103+
}
104+
if (wordsDict[k] == word2) {
105+
j = k;
106+
}
107+
if (i != -1 && j != -1) {
108+
ans = min(ans, abs(i - j));
109+
}
97110
}
111+
return ans;
98112
}
99-
return dp.pop();
113+
};
114+
```
115+
116+
### **Go**
117+
118+
```go
119+
func shortestDistance(wordsDict []string, word1 string, word2 string) int {
120+
ans := 0x3f3f3f3f
121+
i, j := -1, -1
122+
for k, w := range wordsDict {
123+
if w == word1 {
124+
i = k
125+
}
126+
if w == word2 {
127+
j = k
128+
}
129+
if i != -1 && j != -1 {
130+
ans = min(ans, abs(i-j))
131+
}
132+
}
133+
return ans
134+
}
135+
136+
func min(a, b int) int {
137+
if a < b {
138+
return a
139+
}
140+
return b
141+
}
142+
143+
func abs(x int) int {
144+
if x < 0 {
145+
return -x
146+
}
147+
return x
100148
}
101149
```
102150

solution/0200-0299/0243.Shortest Word Distance/README_EN.md

+72-28
Original file line numberDiff line numberDiff line change
@@ -41,51 +41,95 @@
4141
```python
4242
class Solution:
4343
def shortestDistance(self, wordsDict: List[str], word1: str, word2: str) -> int:
44-
i1 = i2 = -1
45-
shortest_distance = len(wordsDict)
46-
for i in range(len(wordsDict)):
47-
if wordsDict[i] == word1:
48-
i1 = i
49-
elif wordsDict[i] == word2:
50-
i2 = i
51-
if i1 != -1 and i2 != -1:
52-
shortest_distance = min(shortest_distance, abs(i1 - i2))
53-
return shortest_distance
44+
i = j = -1
45+
ans = inf
46+
for k, w in enumerate(wordsDict):
47+
if w == word1:
48+
i = k
49+
if w == word2:
50+
j = k
51+
if i != -1 and j != -1:
52+
ans = min(ans, abs(i - j))
53+
return ans
5454
```
5555

5656
### **Java**
5757

5858
```java
5959
class Solution {
6060
public int shortestDistance(String[] wordsDict, String word1, String word2) {
61-
int i1 = -1, i2 = -1;
62-
int shortestDistance = wordsDict.length;
63-
for (int i = 0; i < wordsDict.length; ++i) {
64-
if (word1.equals(wordsDict[i])) {
65-
i1 = i;
66-
} else if (word2.equals(wordsDict[i])) {
67-
i2 = i;
61+
int ans = 0x3f3f3f3f;
62+
for (int k = 0, i = -1, j = -1; k < wordsDict.length; ++k) {
63+
if (wordsDict[k].equals(word1)) {
64+
i = k;
6865
}
69-
if (i1 != -1 && i2 != -1) {
70-
shortestDistance = Math.min(shortestDistance, Math.abs(i1 - i2));
66+
if (wordsDict[k].equals(word2)) {
67+
j = k;
68+
}
69+
if (i != -1 && j != -1) {
70+
ans = Math.min(ans, Math.abs(i - j));
7171
}
7272
}
73-
return shortestDistance;
73+
return ans;
7474
}
7575
}
7676
```
7777

78-
### **TypeScript**
78+
### **C++**
7979

80-
```ts
81-
function integerBreak(n: number): number {
82-
let dp = new Array(n + 1).fill(1);
83-
for (let i = 3; i <= n; i++) {
84-
for (let j = 1; j < i; j++) {
85-
dp[i] = Math.max(dp[i], j * (i - j), j * dp[i - j]);
80+
```cpp
81+
class Solution {
82+
public:
83+
int shortestDistance(vector<string>& wordsDict, string word1, string word2) {
84+
int ans = INT_MAX;
85+
for (int k = 0, i = -1, j = -1; k < wordsDict.size(); ++k) {
86+
if (wordsDict[k] == word1) {
87+
i = k;
88+
}
89+
if (wordsDict[k] == word2) {
90+
j = k;
91+
}
92+
if (i != -1 && j != -1) {
93+
ans = min(ans, abs(i - j));
94+
}
8695
}
96+
return ans;
8797
}
88-
return dp.pop();
98+
};
99+
```
100+
101+
### **Go**
102+
103+
```go
104+
func shortestDistance(wordsDict []string, word1 string, word2 string) int {
105+
ans := 0x3f3f3f3f
106+
i, j := -1, -1
107+
for k, w := range wordsDict {
108+
if w == word1 {
109+
i = k
110+
}
111+
if w == word2 {
112+
j = k
113+
}
114+
if i != -1 && j != -1 {
115+
ans = min(ans, abs(i-j))
116+
}
117+
}
118+
return ans
119+
}
120+
121+
func min(a, b int) int {
122+
if a < b {
123+
return a
124+
}
125+
return b
126+
}
127+
128+
func abs(x int) int {
129+
if x < 0 {
130+
return -x
131+
}
132+
return x
89133
}
90134
```
91135

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int shortestDistance(vector<string>& wordsDict, string word1, string word2) {
4+
int ans = INT_MAX;
5+
for (int k = 0, i = -1, j = -1; k < wordsDict.size(); ++k) {
6+
if (wordsDict[k] == word1) {
7+
i = k;
8+
}
9+
if (wordsDict[k] == word2) {
10+
j = k;
11+
}
12+
if (i != -1 && j != -1) {
13+
ans = min(ans, abs(i - j));
14+
}
15+
}
16+
return ans;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
func shortestDistance(wordsDict []string, word1 string, word2 string) int {
2+
ans := 0x3f3f3f3f
3+
i, j := -1, -1
4+
for k, w := range wordsDict {
5+
if w == word1 {
6+
i = k
7+
}
8+
if w == word2 {
9+
j = k
10+
}
11+
if i != -1 && j != -1 {
12+
ans = min(ans, abs(i-j))
13+
}
14+
}
15+
return ans
16+
}
17+
18+
func min(a, b int) int {
19+
if a < b {
20+
return a
21+
}
22+
return b
23+
}
24+
25+
func abs(x int) int {
26+
if x < 0 {
27+
return -x
28+
}
29+
return x
30+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
class Solution {
22
public int shortestDistance(String[] wordsDict, String word1, String word2) {
3-
int i1 = -1, i2 = -1;
4-
int shortestDistance = wordsDict.length;
5-
for (int i = 0; i < wordsDict.length; ++i) {
6-
if (word1.equals(wordsDict[i])) {
7-
i1 = i;
8-
} else if (word2.equals(wordsDict[i])) {
9-
i2 = i;
3+
int ans = 0x3f3f3f3f;
4+
for (int k = 0, i = -1, j = -1; k < wordsDict.length; ++k) {
5+
if (wordsDict[k].equals(word1)) {
6+
i = k;
107
}
11-
if (i1 != -1 && i2 != -1) {
12-
shortestDistance = Math.min(shortestDistance, Math.abs(i1 - i2));
8+
if (wordsDict[k].equals(word2)) {
9+
j = k;
10+
}
11+
if (i != -1 && j != -1) {
12+
ans = Math.min(ans, Math.abs(i - j));
1313
}
1414
}
15-
return shortestDistance;
15+
return ans;
1616
}
1717
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution:
22
def shortestDistance(self, wordsDict: List[str], word1: str, word2: str) -> int:
3-
i1 = i2 = -1
4-
shortest_distance = len(wordsDict)
5-
for i in range(len(wordsDict)):
6-
if wordsDict[i] == word1:
7-
i1 = i
8-
elif wordsDict[i] == word2:
9-
i2 = i
10-
if i1 != -1 and i2 != -1:
11-
shortest_distance = min(shortest_distance, abs(i1 - i2))
12-
return shortest_distance
3+
i = j = -1
4+
ans = inf
5+
for k, w in enumerate(wordsDict):
6+
if w == word1:
7+
i = k
8+
if w == word2:
9+
j = k
10+
if i != -1 and j != -1:
11+
ans = min(ans, abs(i - j))
12+
return ans

0 commit comments

Comments
 (0)