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

Commitd3ae2ba

Browse files
refactor 244
1 parent371f407 commitd3ae2ba

File tree

1 file changed

+34
-33
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+34
-33
lines changed

‎src/main/java/com/fishercoder/solutions/_244.java

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
importjava.util.Map;
77

88
/**
9+
* 244. Shortest Word Distance II
10+
*
911
* This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?
10-
11-
Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list.
12+
* Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list.
1213
1314
For example,
1415
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
@@ -20,39 +21,39 @@
2021
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
2122
*/
2223
publicclass_244 {
23-
24-
privateMap<String,List<Integer>>map;
25-
26-
public_244(String[]words) {
27-
map =newHashMap<>();
28-
for (inti =0;i <words.length;i++) {
29-
Stringw =words[i];
30-
if (map.containsKey(w)) {
31-
map.get(w).add(i);
32-
}else {
33-
List<Integer>list =newArrayList<>();
34-
list.add(i);
35-
map.put(w,list);
24+
publicstaticclassSolution1 {
25+
classWordDistance {
26+
privateMap<String,List<Integer>>map;
27+
publicWordDistance(String[]words) {
28+
map =newHashMap<>();
29+
for (inti =0;i <words.length;i++) {
30+
Stringw =words[i];
31+
if (map.containsKey(w)) {
32+
map.get(w).add(i);
33+
}else {
34+
List<Integer>list =newArrayList<>();
35+
list.add(i);
36+
map.put(w,list);
37+
}
38+
}
3639
}
37-
}
38-
}
39-
40-
publicintshortest(Stringword1,Stringword2) {
41-
List<Integer>list1 =map.get(word1);
42-
List<Integer>list2 =map.get(word2);
43-
intresult =Integer.MAX_VALUE;
44-
for (inti =0,j =0;i <list1.size() &&j <list2.size();) {
45-
intindex1 =list1.get(i);
46-
intindex2 =list2.get(j);
47-
if (index1 <index2) {
48-
result =Math.min(result,index2 -index1);
49-
i++;
50-
}else {
51-
result =Math.min(result,index1 -index2);
52-
j++;
40+
publicintshortest(Stringword1,Stringword2) {
41+
List<Integer>list1 =map.get(word1);
42+
List<Integer>list2 =map.get(word2);
43+
intresult =Integer.MAX_VALUE;
44+
for (inti =0,j =0;i <list1.size() &&j <list2.size(); ) {
45+
intindex1 =list1.get(i);
46+
intindex2 =list2.get(j);
47+
if (index1 <index2) {
48+
result =Math.min(result,index2 -index1);
49+
i++;
50+
}else {
51+
result =Math.min(result,index1 -index2);
52+
j++;
53+
}
54+
}
55+
returnresult;
5356
}
5457
}
55-
returnresult;
5658
}
57-
5859
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp