Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit317bab4

Browse files
refactor 243
1 parentbe4cabc commit317bab4

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
packagecom.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
publicclass_243 {
14-
15-
publicintshortestDistance(String[]words,Stringword1,Stringword2) {
16-
17-
intp = -1;
18-
intq =-1;
19-
intmin =Integer.MAX_VALUE;
20-
for (inti =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+
publicstaticclassSolution1 {
18+
publicintshortestDistance(String[]words,Stringword1,Stringword2) {
19+
intp = -1;
20+
intq = -1;
21+
intmin =Integer.MAX_VALUE;
22+
for (inti =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-
returnmin;
33+
returnmin;
3234

35+
}
3336
}
34-
3537
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp