forked from algorithm022/algorithm022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLadderLength.java
More file actions
70 lines (64 loc) · 2.26 KB
/
Copy pathLadderLength.java
File metadata and controls
70 lines (64 loc) · 2.26 KB
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
package practice.graph;
import java.util.*;
public class LadderLength {
Map<String, Integer> wordId = new HashMap<String, Integer>();
List<List<Integer>> edge = new ArrayList<List<Integer>>();
int nodeNum = 0;
/**
* 广度优先遍历
* 1) 用计数器 + HashMap<String, Integer> 将string -> int
* 2) 构建邻接矩阵: hot -> *ot, h*t, ho* -> hot, 用List<List<Integer>>, 下标作为映射
* 3) 构建距离数组: int[] dis 初始化为大无穷, 注意!! dis[0] = 0; !!注意,这里需要初始起始距离,下标作为映射, 仅当dis[i] == Integer.MAX时,更新距离, 防止无穷bfs
* @param beginWord
* @param endWord
* @param wordList
* @return
*/
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
for (String word : wordList)
addEdge(word);
addEdge(beginWord);
if (!wordId.containsKey(endWord)) return 0;
int[] dis = new int[nodeNum];
Arrays.fill(dis, Integer.MAX_VALUE);
int beginId = wordId.get(beginWord), endId = wordId.get(endWord);
dis[beginId] = 0;
Queue<Integer> que = new LinkedList<>();
que.offer(beginId);
while (!que.isEmpty()) {
int x = que.poll();
if (x == endId) {
return dis[endId] / 2 + 1;
}
for (int it : edge.get(x)) {
if (dis[it] == Integer.MAX_VALUE) {
dis[it] = dis[x] + 1;
que.offer(it);
}
}
}
return 0;
}
public void addEdge(String word) {
addWord(word);
int id1 = wordId.get(word);
char[] array = word.toCharArray();
int length = array.length;
for (int i = 0; i < length; ++i) {
char tmp = array[i];
array[i] = '*';
String newWord = new String(array);
addWord(newWord);
int id2 = wordId.get(newWord);
edge.get(id1).add(id2);
edge.get(id2).add(id1);
array[i] = tmp;
}
}
public void addWord(String word) {
if (!wordId.containsKey(word)) {
wordId.put(word, nodeNum++);
edge.add(new ArrayList<>());
}
}
}