Skip to content

Commit 317bab4

Browse files
refactor 243
1 parent be4cabc commit 317bab4

File tree

1 file changed

+21
-19
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+21
-19
lines changed
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.fishercoder.solutions;
22

3-
/**Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
3+
/**
4+
* 243. Shortest Word Distance
5+
*
6+
* Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
47
58
For example,
69
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
@@ -11,25 +14,24 @@
1114
Note:
1215
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.*/
1316
public class _243 {
14-
15-
public int shortestDistance(String[] words, String word1, String word2) {
16-
17-
int p = -1;
18-
int q = -1;
19-
int min = Integer.MAX_VALUE;
20-
for (int i = 0; i < words.length; i++) {
21-
if (words[i].equals(word1)) {
22-
p = i;
23-
}
24-
if (words[i].equals(word2)) {
25-
q = i;
26-
}
27-
if (p != -1 && q != -1) {
28-
min = Math.min(min, Math.abs(p - q));
17+
public static class Solution1 {
18+
public int shortestDistance(String[] words, String word1, String word2) {
19+
int p = -1;
20+
int q = -1;
21+
int min = Integer.MAX_VALUE;
22+
for (int i = 0; i < words.length; i++) {
23+
if (words[i].equals(word1)) {
24+
p = i;
25+
}
26+
if (words[i].equals(word2)) {
27+
q = i;
28+
}
29+
if (p != -1 && q != -1) {
30+
min = Math.min(min, Math.abs(p - q));
31+
}
2932
}
30-
}
31-
return min;
33+
return min;
3234

35+
}
3336
}
34-
3537
}

0 commit comments

Comments
 (0)