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

Commit7401a04

Browse files
refactor 599
1 parent72e6a3f commit7401a04

File tree

1 file changed

+30
-28
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+30
-28
lines changed

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

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,40 @@
3434
No duplicates in both lists.
3535
*/
3636
publicclass_599 {
37-
publicString[]findRestaurant(String[]list1,String[]list2) {
38-
if (list1 ==null ||list2 ==null) {
39-
returnnewString[0];
40-
}
41-
Map<String,Integer>map1 =putIntoMap(list1);
42-
Map<String,Integer>map2 =putIntoMap(list2);
43-
intleastIndexSum =Integer.MAX_VALUE;
44-
List<String>resultList =newArrayList<>();
45-
for (Stringkey1 :map1.keySet()) {
46-
if (map2.containsKey(key1)) {
47-
intindexSum =map1.get(key1) +map2.get(key1);
48-
if (indexSum <leastIndexSum) {
49-
resultList.clear();
50-
resultList.add(key1);
51-
leastIndexSum =indexSum;
52-
}elseif (indexSum ==leastIndexSum) {
53-
resultList.add(key1);
37+
publicstaticclassSolution1 {
38+
publicString[]findRestaurant(String[]list1,String[]list2) {
39+
if (list1 ==null ||list2 ==null) {
40+
returnnewString[0];
41+
}
42+
Map<String,Integer>map1 =putIntoMap(list1);
43+
Map<String,Integer>map2 =putIntoMap(list2);
44+
intleastIndexSum =Integer.MAX_VALUE;
45+
List<String>resultList =newArrayList<>();
46+
for (Stringkey1 :map1.keySet()) {
47+
if (map2.containsKey(key1)) {
48+
intindexSum =map1.get(key1) +map2.get(key1);
49+
if (indexSum <leastIndexSum) {
50+
resultList.clear();
51+
resultList.add(key1);
52+
leastIndexSum =indexSum;
53+
}elseif (indexSum ==leastIndexSum) {
54+
resultList.add(key1);
55+
}
5456
}
5557
}
58+
String[]result =newString[resultList.size()];
59+
for (inti =0;i <resultList.size();i++) {
60+
result[i] =resultList.get(i);
61+
}
62+
returnresult;
5663
}
57-
String[]result =newString[resultList.size()];
58-
for (inti =0;i <resultList.size();i++) {
59-
result[i] =resultList.get(i);
60-
}
61-
returnresult;
62-
}
6364

64-
privateMap<String,Integer>putIntoMap(String[]list) {
65-
Map<String,Integer>map =newHashMap<>();
66-
for (inti =0;i <list.length;i++) {
67-
map.put(list[i],i);
65+
privateMap<String,Integer>putIntoMap(String[]list) {
66+
Map<String,Integer>map =newHashMap<>();
67+
for (inti =0;i <list.length;i++) {
68+
map.put(list[i],i);
69+
}
70+
returnmap;
6871
}
69-
returnmap;
7072
}
7173
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp