-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution_126.java
149 lines (133 loc) · 5.05 KB
/
Solution_126.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
126. 单词接龙 II
给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列。转换需遵循如下规则:
每次转换只能改变一个字母。
转换过程中的中间单词必须是字典中的单词。
说明:
如果不存在这样的转换序列,返回一个空列表。
所有单词具有相同的长度。
所有单词只由小写字母组成。
字典中不存在重复的单词。
你可以假设 beginWord 和 endWord 是非空的,且二者不相同。
示例 1:
输入:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
输出:
[
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]
示例 2:
输入:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
输出: []
解释: endWord "cog" 不在字典中,所以不存在符合要求的转换序列。
*/
import java.util.*;
import java.util.List;
public class Solution {
public static void main(String[] args) {
String beginword = "hit";
String endword = "cog";
List<String> wordlist = new ArrayList<>();
wordlist.add("hot");
wordlist.add("dot");
wordlist.add("dog");
wordlist.add("lot");
wordlist.add("log");
wordlist.add("cog");
Solution solution = new Solution();
System.out.println(solution.findLadders(beginword, endword, wordlist));
}
public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
List<List<String>> ans = new ArrayList<>();
// 如果不含有结束单词,直接结束,不然后边会造成死循环
if (!wordList.contains(endWord)) {
return ans;
}
// 利用 BFS 得到所有的邻居节点,以及每个节点的所在层数
HashMap<String, Integer> distance = new HashMap<>();
HashMap<String, ArrayList<String>> map = new HashMap<>();
bfs(beginWord, endWord, wordList, map, distance);
ArrayList<String> temp = new ArrayList<String>();
// temp 用来保存当前的路径
temp.add(beginWord);
findLaddersHelper(beginWord, endWord, map, distance, temp, ans);
return ans;
}
private void findLaddersHelper(String beginWord, String endWord, HashMap<String, ArrayList<String>> map,
HashMap<String, Integer> distance, ArrayList<String> temp, List<List<String>> ans) {
if (beginWord.equals(endWord)) {
ans.add(new ArrayList<String>(temp));
return;
}
// 得到所有的下一个的节点
/*
"a"
"c"
["a","b","c"]*/
//之所以是 map.getOrDefault 而不是 get,就是上边的情况 get 会出错
ArrayList<String> neighbors = map.getOrDefault(beginWord, new ArrayList<String>());
for (String neighbor : neighbors) {
//判断层数是否符合
if (distance.get(beginWord) + 1 == distance.get(neighbor)) {
temp.add(neighbor);
findLaddersHelper(neighbor, endWord, map, distance, temp, ans);
temp.remove(temp.size() - 1);
}
}
}
public void bfs(String beginWord, String endWord, List<String> wordList, HashMap<String, ArrayList<String>> map,
HashMap<String, Integer> distance) {
Queue<String> queue = new LinkedList<>();
queue.offer(beginWord);
distance.put(beginWord, 0);
boolean isFound = false;
int depth = 0;
Set<String> dict = new HashSet<>(wordList);
while (!queue.isEmpty()) {
int size = queue.size();
depth++;
for (int j = 0; j < size; j++) {
String temp = queue.poll();
// 一次性得到所有的下一个的节点
ArrayList<String> neighbors = getNeighbors(temp, dict);
map.put(temp, neighbors);
for (String neighbor : neighbors) {
if (!distance.containsKey(neighbor)) {
distance.put(neighbor, depth);
if (neighbor.equals(endWord)) {
isFound = true;
}
queue.offer(neighbor);
}
}
}
if (isFound) {
break;
}
}
}
private ArrayList<String> getNeighbors(String node, Set<String> dict) {
ArrayList<String> res = new ArrayList<String>();
char chs[] = node.toCharArray();
for (char ch = 'a'; ch <= 'z'; ch++) {
for (int i = 0; i < chs.length; i++) {
if (chs[i] == ch)
continue;
char old_ch = chs[i];
chs[i] = ch;
if (dict.contains(String.valueOf(chs))) {
res.add(String.valueOf(chs));
}
chs[i] = old_ch;
}
}
//System.out.println(node+" : "+res.toString());
return res;
}
}