-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathFindLadders.java
More file actions
117 lines (106 loc) · 3.76 KB
/
FindLadders.java
File metadata and controls
117 lines (106 loc) · 3.76 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
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
package com.lga.algorithm.tag.homework.Week_04;
import org.junit.Test;
import java.util.*;
/**
* 126. 单词接龙 II
* https://leetcode-cn.com/problems/word-ladder-ii/description/
*/
public class FindLadders {
List<List<String>> ans = new LinkedList<>();
int minSetp = Integer.MAX_VALUE;
public List<List<String>> findLadders_backtance(String beginWord, String endWord, List<String> wordList) {
if(beginWord.length() != endWord.length() || !wordList.contains(endWord)) return ans;
LinkedList<String> trace = new LinkedList<>();
trace.add(beginWord);
int setp = 1;
backtrace(beginWord,endWord,wordList,trace,setp);
return ans;
}
/**
* 回溯算法
* @param beginWord
* @param endWord
* @param wordList
* @param trace
*/
private void backtrace(String beginWord, String endWord, List<String> wordList, LinkedList<String> trace,int step) {
if (trace.getLast().equals(endWord) && step <= minSetp) {
//之前的最小个数
int tempStep = minSetp;
//之后的最小个数
minSetp = Math.min(minSetp, step);
//移除之前的最小个数的元素
if (minSetp < tempStep) {
Iterator<List<String>> iterator = ans.iterator();
while (iterator.hasNext()) {
List<String> list = iterator.next();
if (list.size() == tempStep) iterator.remove();
}
}
ans.add(new LinkedList<>(trace));
return;
}
for (String str : wordList) {
if (trace.contains(str)) continue;
if (!convert(str,beginWord)) continue;
trace.add(str);
step++;
backtrace(str,endWord,wordList,trace,step);
trace.removeLast();
step--;
}
}
private boolean convert(String str, String beginWord) {
int diff = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != beginWord.charAt(i))
if(++diff>1) return false;
}
return true;
}
/**
* 层次遍历
* @param beginWord
* @param endWord
* @param wordList
* @return
*/
public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
if(beginWord.length() != endWord.length() || !wordList.contains(endWord)) return ans;
Queue<String> queue = new LinkedList<>();
queue.add(beginWord);
Set<String> visitedSet = new HashSet<>();
visitedSet.add(beginWord);
LinkedList<String> list = new LinkedList<>();
//key:子节点, value:父节点
Map<String, String> parentMap = new HashMap<>();
parentMap.put(beginWord, "NONE");
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- > 0) {
String poll = queue.poll();
list.add(poll);
if (endWord.equals(poll)) {
ans.add(new LinkedList<>(list));
}
for (String str : wordList) {
if (visitedSet.contains(str)) continue;
if (!convert(poll,str)) continue;
queue.add(str);
visitedSet.add(str);
parentMap.put(str, poll);
}
}
// if (completed) return ans;
}
while (!"NONE".equals(parentMap.get(endWord))) {
System.out.print(endWord+"<-");
endWord = parentMap.get(endWord);
}
return ans;
}
@Test
public void test() {
System.out.println(findLadders("hit", "cog", Arrays.asList("hot","dot","dog","lot","log","cog")));
}
}