39
39
40
40
<!-- 这里可写通用的实现逻辑 -->
41
41
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 ` 的长度。
43
47
44
48
<!-- tabs:start -->
45
49
50
54
``` python
51
55
class Solution :
52
56
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
63
67
```
64
68
65
69
### ** Java**
@@ -69,34 +73,78 @@ class Solution:
69
73
``` java
70
74
class Solution {
71
75
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;
79
80
}
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));
82
86
}
83
87
}
84
- return shortestDistance ;
88
+ return ans ;
85
89
}
86
90
}
87
91
```
88
92
89
- ### ** TypeScript **
93
+ ### ** C++ **
90
94
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
+ }
97
110
}
111
+ return ans;
98
112
}
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
100
148
}
101
149
```
102
150
0 commit comments