Skip to content

Commit 58ec94d

Browse files
committed
fd
1 parent 3e93d1a commit 58ec94d

File tree

2 files changed

+67
-53
lines changed

2 files changed

+67
-53
lines changed

leetcode/solution/src/WordLadder.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,46 @@ public class WordLadder {
1515
/**
1616
* 要注意添加节点时要给单词从dict中删掉
1717
*/
18-
// 常规的BFS,耗时141ms
18+
// 常规的BFS,耗时64ms
1919
public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
20-
wordList.remove(beginWord);
21-
wordList.add(endWord);
22-
20+
HashSet<String> words = new HashSet<>(wordList);
2321
Queue<String> queue = new LinkedList<>();
24-
Queue<String> next = new LinkedList<>();
25-
queue.add(beginWord);
22+
queue.offer(beginWord);
2623

27-
int ladder = 1;
24+
Queue<String> next = new LinkedList<>();
25+
int length = 1;
2826

2927
while (!queue.isEmpty()) {
30-
String word = queue.poll();
31-
32-
StringBuilder sb = new StringBuilder(word);
33-
for (int i = 0; i < word.length(); i++) {
34-
char c = word.charAt(i);
35-
for (int j = 0; j < 26; j++) {
36-
if (j + 'a' == c) {
37-
continue;
38-
}
39-
sb.setCharAt(i, (char) (j + 'a'));
40-
String s = sb.toString();
28+
String s = queue.poll();
4129

42-
if (s.equals(endWord)) {
43-
return ladder + 1;
44-
}
30+
if (s.equals(endWord)) {
31+
return length;
32+
}
33+
34+
if (!words.isEmpty()) {
35+
StringBuilder sb = new StringBuilder(s);
36+
for (int i = 0; i < s.length(); i++) {
37+
for (char c = 'a'; c <= 'z'; c++) {
38+
if (c == s.charAt(i)) {
39+
continue;
40+
}
41+
42+
sb.setCharAt(i, c);
43+
String st = sb.toString();
4544

46-
if (wordList.remove(s)) {
47-
next.add(s);
45+
if (words.contains(st)) {
46+
next.offer(st);
47+
words.remove(st);
48+
}
4849
}
50+
sb.setCharAt(i, s.charAt(i));
4951
}
50-
sb.setCharAt(i, c);
5152
}
5253

5354
if (queue.isEmpty()) {
5455
queue.addAll(next);
5556
next.clear();
56-
ladder++;
57+
length++;
5758
}
5859
}
5960

leetcode/src/Main.java

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,58 @@ public class Main {
44

55
public static class Solution {
66

7-
public List<String> restoreIpAddresses(String s) {
8-
List<String> result = new ArrayList<>();
9-
dfs(s, 0, new LinkedList<>(), result);
10-
return new ArrayList<>(result);
11-
}
7+
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
8+
HashSet<String> words = new HashSet<>(wordList);
9+
Queue<String> queue = new LinkedList<>();
10+
queue.offer(beginWord);
1211

13-
private void dfs(String s, int index, Deque<String> ips, List<String> result) {
14-
if (ips.size() > 4) {
15-
return;
16-
}
17-
if (index >= s.length()) {
18-
if (ips.size() == 4) {
19-
result.add(String.join(".", ips));
12+
Queue<String> next = new LinkedList<>();
13+
int length = 1;
14+
15+
while (!queue.isEmpty()) {
16+
String s = queue.poll();
17+
18+
if (s.equals(endWord)) {
19+
return length;
2020
}
21-
return;
22-
}
23-
for (int i = 1; i <= 3 && index + i <= s.length(); i++) {
24-
String t = s.substring(index, index + i);
25-
int k = Integer.parseInt(t);
26-
if (i == 3 && k > 255) {
27-
break;
21+
22+
if (!words.isEmpty()) {
23+
StringBuilder sb = new StringBuilder(s);
24+
for (int i = 0; i < s.length(); i++) {
25+
for (char c = 'a'; c <= 'z'; c++) {
26+
if (c == s.charAt(i)) {
27+
continue;
28+
}
29+
30+
sb.setCharAt(i, c);
31+
String st = sb.toString();
32+
33+
if (words.contains(st)) {
34+
next.offer(st);
35+
words.remove(st);
36+
}
37+
}
38+
sb.setCharAt(i, s.charAt(i));
39+
}
2840
}
29-
ips.offer(t);
30-
dfs(s, index + i, ips, result);
31-
ips.pollLast();
3241

33-
if (k == 0) {
34-
break;
42+
if (queue.isEmpty()) {
43+
queue.addAll(next);
44+
next.clear();
45+
length++;
3546
}
3647
}
48+
49+
return 0;
3750
}
3851
}
3952

4053

4154
public static void main(String[] args) {
4255
Solution solution = new Solution();
43-
List<String> result = solution.restoreIpAddresses("111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111");
44-
for (String s : result) {
45-
System.out.println(s);
46-
}
56+
int len = solution.ladderLength("hit", "cog", Arrays.asList(new String[]{
57+
"hot", "dot", "dog", "lot", "log"
58+
}));
59+
System.out.println(len);
4760
}
4861
}

0 commit comments

Comments
 (0)