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

Commitb440677

Browse files
refactor 642
1 parentec0b396 commitb440677

File tree

1 file changed

+41
-38
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+41
-38
lines changed

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

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -63,55 +63,58 @@ List<String> input(char c): The input c is the next character typed by the user.
6363
Please remember to RESET your class variables declared in class AutocompleteSystem, as static/class variables are persisted across multiple test cases. Please see here for more details.
6464
*/
6565
publicclass_642 {
66+
publicstaticclassSolution1 {
6667

67-
/**reference: https://discuss.leetcode.com/topic/96150/java-solution-trie-and-priorityqueue/3*/
68-
publicclassAutocompleteSystem {
68+
/**
69+
* reference: https://discuss.leetcode.com/topic/96150/java-solution-trie-and-priorityqueue/3
70+
*/
71+
publicclassAutocompleteSystem {
6972

70-
Map<String,Integer>map;
71-
List<Map.Entry<String,Integer>>answers;
72-
StringBuilderstringBuilder;
73+
Map<String,Integer>map;
74+
List<Map.Entry<String,Integer>>answers;
75+
StringBuilderstringBuilder;
7376

74-
publicAutocompleteSystem(String[]sentences,int[]times) {
75-
map =newHashMap<>();
76-
answers =newArrayList<>();
77-
stringBuilder =newStringBuilder();
77+
publicAutocompleteSystem(String[]sentences,int[]times) {
78+
map =newHashMap<>();
79+
answers =newArrayList<>();
80+
stringBuilder =newStringBuilder();
7881

79-
for (inti =0;i <sentences.length;i++) {
80-
map.put(sentences[i],map.getOrDefault(sentences[i],0) +times[i]);
82+
for (inti =0;i <sentences.length;i++) {
83+
map.put(sentences[i],map.getOrDefault(sentences[i],0) +times[i]);
84+
}
8185
}
82-
}
8386

84-
publicList<String>input(charc) {
85-
List<String>result =newArrayList<>();
86-
if (c =='#') {
87-
map.put(stringBuilder.toString(),map.getOrDefault(stringBuilder.toString(),0) +1);
88-
stringBuilder.setLength(0);
89-
answers.clear();/**The user has finished typing, so we'll clean answers to get ready for next search*/
90-
}else {
91-
stringBuilder.append(c);
92-
/**when its length is 1, we find all the prefix that is a match and put them into answers,
93-
* then for the rest, we'll just remove those that are not match with the prefix any more, we do this logic in else branch*/
94-
if (stringBuilder.length() ==1) {
95-
for (Map.Entry<String,Integer>entry :map.entrySet()) {
96-
if (entry.getKey().startsWith(stringBuilder.toString())) {
97-
answers.add(entry);
98-
}
99-
}
100-
Collections.sort(answers, (a,b) ->a.getValue() ==b.getValue() ?a.getKey().compareTo(b.getKey()) :b.getValue() -a.getValue());
87+
publicList<String>input(charc) {
88+
List<String>result =newArrayList<>();
89+
if (c =='#') {
90+
map.put(stringBuilder.toString(),map.getOrDefault(stringBuilder.toString(),0) +1);
91+
stringBuilder.setLength(0);
92+
answers.clear();/**The user has finished typing, so we'll clean answers to get ready for next search*/
10193
}else {
102-
for (Iterator<Map.Entry<String,Integer>>iterator =answers.iterator();iterator.hasNext();) {
103-
if (!iterator.next().getKey().startsWith(stringBuilder.toString())) {
104-
iterator.remove();
94+
stringBuilder.append(c);
95+
/**when its length is 1, we find all the prefix that is a match and put them into answers,
96+
* then for the rest, we'll just remove those that are not match with the prefix any more, we do this logic in else branch*/
97+
if (stringBuilder.length() ==1) {
98+
for (Map.Entry<String,Integer>entry :map.entrySet()) {
99+
if (entry.getKey().startsWith(stringBuilder.toString())) {
100+
answers.add(entry);
101+
}
102+
}
103+
Collections.sort(answers, (a,b) ->a.getValue() ==b.getValue() ?a.getKey().compareTo(b.getKey()) :b.getValue() -a.getValue());
104+
}else {
105+
for (Iterator<Map.Entry<String,Integer>>iterator =answers.iterator();iterator.hasNext(); ) {
106+
if (!iterator.next().getKey().startsWith(stringBuilder.toString())) {
107+
iterator.remove();
108+
}
105109
}
106110
}
111+
for (inti =0;i <3 &&i <answers.size();i++) {
112+
result.add(answers.get(i).getKey());
113+
}
107114
}
108-
109-
for (inti =0;i <3 &&i <answers.size();i++) {
110-
result.add(answers.get(i).getKey());
111-
}
115+
returnresult;
112116
}
113-
returnresult;
114117
}
115-
}
116118

119+
}
117120
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp