Skip to content

Commit 05dddf2

Browse files
committedOct 4, 2018
Add solution 127
1 parent 3c09fdc commit 05dddf2

File tree

3 files changed

+149
-2
lines changed

3 files changed

+149
-2
lines changed
 

‎README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Complete solutions to Leetcode problems, updated daily.
3636
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://github.com/doocs/leetcode/tree/master/solution/235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree) | `Tree` |
3737
| 237 | [Delete Node in a Linked List](https://github.com/doocs/leetcode/tree/master/solution/237.Delete%20Node%20in%20a%20Linked%20List) | `Linked List` |
3838
| 344 | [Reverse String](https://github.com/doocs/leetcode/tree/master/solution/344.Reverse%20String) | `Two Pointers`, `String` |
39-
| 703 | [Kth Largest Element in a Stream](%20ttps://github.com/doocs/leetcode/tree/master/solution/703.Kth%20Largest%20Element%20in%20a%20Stream) | `Heap` |
39+
| 703 | [Kth Largest Element in a Stream](https://github.com/doocs/leetcode/tree/master/solution/703.Kth%20Largest%20Element%20in%20a%20Stream) | `Heap` |
4040
| 876 | [Middle of the Linked List](https://github.com/doocs/leetcode/tree/master/solution/876.Middle%20of%20the%20Linked%20List) | `Linked List` |
4141

4242

@@ -58,6 +58,7 @@ Complete solutions to Leetcode problems, updated daily.
5858
| 086 | [Partition List](https://github.com/doocs/leetcode/tree/master/solution/086.Partition%20List) | `Linked List`, `Two Pointers` |
5959
| 092 | [Reverse Linked List II](https://github.com/doocs/leetcode/tree/master/solution/092.Reverse%20Linked%20List%20II) | `Linked List` |
6060
| 094 | [Binary Tree Inorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/094.Binary%20Tree%20Inorder%20Traversal) | `Hash Table`, `Stack`, `Tree` |
61+
| 127 | [Word Ladder](https://github.com/doocs/leetcode/tree/master/solution/127.Word%20Ladder) | `Breadth-first Search` |
6162
| 144 | [Binary Tree Preorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/144.Binary%20Tree%20Preorder%20Traversal) | `Stack`, `Tree` |
6263
| 150 | [Evaluate Reverse Polish Notation](https://github.com/doocs/leetcode/tree/master/solution/150.Evaluate%20Reverse%20Polish%20Notation) | `Stack` |
6364
| 153 | [Find Minimum in Rotated Sorted Array](https://github.com/doocs/leetcode/tree/master/solution/153.Find%20Minimum%20in%20Rotated%20Sorted%20Array) | `Array`, `Binary Search` |
@@ -81,7 +82,7 @@ I'm looking for long-term contributors/partners to this repo! Send me PRs if you
8182
## Contributors
8283

8384
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
84-
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>doocs</b></sub>](https://github.com/doocs)<br />[💻](https://github.com/doocs/leetcode/commits?author=doocs "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/23625436?v=4" width="100px;"/><br /><sub><b>chakyam</b></sub>](https://github.com/chakyam)<br />[💻](https://github.com/doocs/leetcode/commits?author=chakyam "Code") </center> |
85+
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/doocs/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/23625436?v=4" width="100px;"/><br /><sub><b>chakyam</b></sub>](https://github.com/chakyam)<br />[💻](https://github.com/doocs/leetcode/commits?author=chakyam "Code") </center> |
8586
|---|---|
8687

8788
<!-- ALL-CONTRIBUTORS-LIST:END -->

‎solution/127.Word Ladder/README.md

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
## 单词接龙
2+
### 题目描述
3+
4+
给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度。转换需遵循如下规则:
5+
6+
- 每次转换只能改变一个字母。
7+
- 转换过程中的中间单词必须是字典中的单词。
8+
9+
说明:
10+
11+
- 如果不存在这样的转换序列,返回 0。
12+
- 所有单词具有相同的长度。
13+
- 所有单词只由小写字母组成。
14+
- 字典中不存在重复的单词。
15+
- 你可以假设 beginWord 和 endWord 是非空的,且二者不相同。
16+
17+
示例 1:
18+
```
19+
输入:
20+
beginWord = "hit",
21+
endWord = "cog",
22+
wordList = ["hot","dot","dog","lot","log","cog"]
23+
24+
输出: 5
25+
26+
解释: 一个最短转换序列是 "hit" -> "hot" -> "dot" -> "dog" -> "cog",
27+
返回它的长度 5。
28+
```
29+
30+
示例 2:
31+
```
32+
输入:
33+
beginWord = "hit"
34+
endWord = "cog"
35+
wordList = ["hot","dot","dog","lot","log"]
36+
37+
输出: 0
38+
39+
解释: endWord "cog" 不在字典中,所以无法进行转换。
40+
```
41+
42+
### 解法
43+
利用广度优先搜索,`level` 表示层数,`curNum` 表示当前层的单词数,`nextNum` 表示下一层的单词数。
44+
队首元素出队,对于该字符串,替换其中每一个字符为[a...z] 中的任一字符。判断新替换后的字符串是否在字典中。若是,若该字符串与 endWord 相等,直接返回 level + 1;若不等,该字符串入队,nextNum + 1,并将 该字符串从 wordSet 移除。
45+
46+
注意,每次只能替换一个字符,因此,在下一次替换前,需恢复为上一次替换前的状态。
47+
48+
```java
49+
class Solution {
50+
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
51+
Queue<String> queue = new LinkedList<>();
52+
53+
// 需要转hashSet
54+
Set<String> wordSet = new HashSet<>(wordList);
55+
queue.offer(beginWord);
56+
int level = 1;
57+
int curNum = 1;
58+
int nextNum = 0;
59+
while (!queue.isEmpty()) {
60+
String s = queue.poll();
61+
--curNum;
62+
char[] chars = s.toCharArray();
63+
for (int i = 0; i < chars.length; ++i) {
64+
char ch = chars[i];
65+
for (char j = 'a'; j <= 'z'; ++j) {
66+
chars[i] = j;
67+
String tmp = new String(chars);
68+
// 字典中包含生成的中间字符串
69+
if (wordSet.contains(tmp)) {
70+
// 中间字符串与 endWord 相等
71+
if (endWord.equals(tmp)) {
72+
return level + 1;
73+
}
74+
75+
// 中间字符串不是 endWord,则入队
76+
queue.offer(tmp);
77+
++nextNum;
78+
79+
// 确保之后不会再保存 tmp 字符串
80+
wordSet.remove(tmp);
81+
}
82+
}
83+
chars[i] = ch;
84+
}
85+
86+
if (curNum == 0) {
87+
curNum = nextNum;
88+
nextNum = 0;
89+
++level;
90+
}
91+
92+
}
93+
94+
return 0;
95+
}
96+
}
97+
98+
```
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
3+
Queue<String> queue = new LinkedList<>();
4+
5+
// 需要转hashSet
6+
Set<String> wordSet = new HashSet<>(wordList);
7+
queue.offer(beginWord);
8+
int level = 1;
9+
int curNum = 1;
10+
int nextNum = 0;
11+
while (!queue.isEmpty()) {
12+
String s = queue.poll();
13+
--curNum;
14+
char[] chars = s.toCharArray();
15+
for (int i = 0; i < chars.length; ++i) {
16+
char ch = chars[i];
17+
for (char j = 'a'; j <= 'z'; ++j) {
18+
chars[i] = j;
19+
String tmp = new String(chars);
20+
// 字典中包含生成的中间字符串
21+
if (wordSet.contains(tmp)) {
22+
// 中间字符串与 endWord 相等
23+
if (endWord.equals(tmp)) {
24+
return level + 1;
25+
}
26+
27+
// 中间字符串不是 endWord,则入队
28+
queue.offer(tmp);
29+
++nextNum;
30+
31+
// 确保之后不会再保存 tmp 字符串
32+
wordSet.remove(tmp);
33+
}
34+
}
35+
chars[i] = ch;
36+
}
37+
38+
if (curNum == 0) {
39+
curNum = nextNum;
40+
nextNum = 0;
41+
++level;
42+
}
43+
44+
}
45+
46+
return 0;
47+
}
48+
}

0 commit comments

Comments
 (0)