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

Commit311dd26

Browse files
format
1 parent6494811 commit311dd26

File tree

1 file changed

+12
-3
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+12
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ public int ladderLength(String beginWord, String endWord, List<String> wordList)
4141
Set<String>startSet =newHashSet<>();
4242
Set<String>endSet =newHashSet<>();
4343
Set<String>visited =newHashSet<>();
44+
4445
startSet.add(beginWord);
45-
if (dict.contains(endWord))endSet.add(endWord);// all transformed words must be in dict (including endWord)
46+
if (dict.contains(endWord)) {
47+
endSet.add(endWord);// all transformed words must be in dict (including endWord)
48+
}
49+
4650
for (intlen =2; !startSet.isEmpty();len++) {
4751
Set<String>nq =newHashSet<>();
4852
for (Stringw :startSet) {
@@ -52,11 +56,16 @@ public int ladderLength(String beginWord, String endWord, List<String> wordList)
5256
if (c ==w.charAt(j))continue;// beginWord and endWord should not be the same
5357
ch[j] =c;
5458
Stringnb =String.valueOf(ch);
55-
if (endSet.contains(nb))returnlen;// meet from two ends
56-
if (dict.contains(nb) &&visited.add(nb))nq.add(nb);// not meet yet, visited is safe to use
59+
if (endSet.contains(nb)) {
60+
returnlen;// meet from two ends
61+
}
62+
if (dict.contains(nb) &&visited.add(nb)) {
63+
nq.add(nb);// not meet yet, visited is safe to use
64+
}
5765
}
5866
}
5967
}
68+
6069
startSet = (nq.size() <endSet.size()) ?nq :endSet;// switch to small one to traverse from other end
6170
endSet = (startSet ==nq) ?endSet :nq;
6271
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp