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

Commit09b1b99

Browse files
update 269
1 parent4e1241b commit09b1b99

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,26 @@ public static class Solution1 {
1414
* reference: https://discuss.leetcode.com/topic/28308/java-ac-solution-using-bfs
1515
*/
1616
publicStringalienOrder(String[]words) {
17-
Map<Character,Set<Character>>map =newHashMap();
17+
Map<Character,Set<Character>>map =newHashMap<>();
1818
Map<Character,Integer>degree =newHashMap<>();
1919
Stringresult ="";
2020
if (words ==null ||words.length ==0) {
2121
returnresult;
2222
}
2323
for (Strings :words) {
2424
for (charc :s.toCharArray()) {
25-
degree.put(c,0);//keeps overwriting it, the purpose is to create one entry
26-
//for each letter in the degree map
25+
degree.put(c,0);
2726
}
2827
}
2928
for (inti =0;i <words.length -1;i++) {
30-
Stringcur =words[i];
29+
Stringcurr =words[i];
3130
Stringnext =words[i +1];
32-
intlength =Math.min(cur.length(),next.length());
33-
for (intj =0;j <length;j++) {
34-
charc1 =cur.charAt(j);
31+
if (curr.length() >next.length() &&curr.startsWith(next)) {
32+
return"";
33+
}
34+
intminLen =Math.min(curr.length(),next.length());
35+
for (intj =0;j <minLen;j++) {
36+
charc1 =curr.charAt(j);
3537
charc2 =next.charAt(j);
3638
if (c1 !=c2) {
3739
Set<Character>set =newHashSet<>();
@@ -50,17 +52,17 @@ public String alienOrder(String[] words) {
5052
Queue<Character>queue =newLinkedList<>();
5153
for (charc :degree.keySet()) {
5254
if (degree.get(c) ==0) {
53-
queue.add(c);
55+
queue.offer(c);
5456
}
5557
}
5658
while (!queue.isEmpty()) {
57-
charc =queue.remove();
58-
result +=c;
59-
if (map.containsKey(c)) {
60-
for (charc2 :map.get(c)) {
61-
degree.put(c2,degree.get(c2) -1);
62-
if (degree.get(c2) ==0) {
63-
queue.add(c2);
59+
charcurr =queue.poll();
60+
result +=curr;
61+
if (map.containsKey(curr)) {
62+
for (charc :map.get(curr)) {
63+
degree.put(c,degree.get(c) -1);
64+
if (degree.get(c) ==0) {
65+
queue.offer(c);
6466
}
6567
}
6668
}

‎src/test/java/com/fishercoder/_269Test.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,10 @@ public void test1() {
2121
assertEquals("wertf",solution1.alienOrder(words));
2222
}
2323

24+
@Test
25+
publicvoidtest2() {
26+
words =newString[]{"abc","ab"};
27+
assertEquals("",solution1.alienOrder(words));
28+
}
29+
2430
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp