Skip to content

Commit 2609786

Browse files
committed
提交第7期作业
1 parent 6e50f14 commit 2609786

3 files changed

Lines changed: 120 additions & 1 deletion

File tree

Week07/LadderLength.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.*;
2+
3+
public class LadderLength {
4+
5+
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
6+
HashSet<String> start = new HashSet<>();
7+
start.add(beginWord);
8+
HashSet<String> end = new HashSet<>();
9+
end.add(endWord);
10+
HashSet<String> words = new HashSet<>(wordList);
11+
if (!words.contains(endWord)) {
12+
return 0;
13+
}
14+
return deBfs(start,end,words,2);
15+
}
16+
private int deBfs(HashSet<String> start, HashSet<String> end, HashSet<String> words, int depth) {
17+
if (start.size() > end.size()) {
18+
return deBfs(end, start, words, depth);
19+
}
20+
words.removeAll(start);
21+
HashSet<String> next = new HashSet<>();
22+
for (String str : start) {
23+
char[] chars = str.toCharArray();
24+
for (int i = 0; i < chars.length; i++) {
25+
char temp = chars[i];
26+
for (char j = 'a'; j <= 'z'; j++) {
27+
chars[i] = j;
28+
String word = new String(chars);
29+
if (words.contains(word)) {
30+
if (end.contains(word)) {
31+
return depth;
32+
}
33+
next.add(word);
34+
}
35+
}
36+
chars[i] = temp;
37+
}
38+
39+
}
40+
if (start.isEmpty()) {
41+
return 0;
42+
}
43+
return deBfs(next, end, words, depth + 1);
44+
}
45+
}

Week07/NOTE.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
1-
学习笔记
1+
学习笔记
2+
3+
```python
4+
def doubleBFS(graph, start, end):
5+
visited = set()
6+
startQueue = []
7+
startQueue.append([start])
8+
while startQueue:
9+
node = startQueue.pop()
10+
visited.add(node)
11+
process(node)
12+
nodes = generate_related_nodes(node)
13+
startQueue.push(nodes)
14+
# other processing work
15+
endQueue = []
16+
endQueue.append([end])
17+
while endQueue:
18+
node = endQueue.pop()
19+
visited.add(node)
20+
process(node)
21+
nodes = generate_related_nodes(node)
22+
endQueue.push(nodes)
23+
# other processing work
24+
retainall(startQueue, endQueue)
25+
```
26+

Week07/Trie.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
public class Trie {
2+
3+
private boolean isEnd;
4+
private Trie[] next;
5+
/** Initialize your data structure here. */
6+
public Trie() {
7+
isEnd = false;
8+
next = new Trie[26];
9+
}
10+
11+
/** Inserts a word into the trie. */
12+
public void insert(String word) {
13+
if (word == null || word.length() == 0) {
14+
return;
15+
}
16+
Trie curr = this;
17+
char[] words = word.toCharArray();
18+
for (int i = 0;i < words.length;i++) {
19+
int n = words[i] - 'a';
20+
if (curr.next[n] == null) {
21+
curr.next[n] = new Trie();
22+
}
23+
curr = curr.next[n];
24+
}
25+
curr.isEnd = true;
26+
}
27+
28+
/** Returns if the word is in the trie. */
29+
public boolean search(String word) {
30+
Trie node = searchPrefix(word);
31+
return node != null && node.isEnd;
32+
}
33+
34+
/** Returns if there is any word in the trie that starts with the given prefix. */
35+
public boolean startsWith(String prefix) {
36+
Trie node = searchPrefix(prefix);
37+
return node != null;
38+
}
39+
40+
private Trie searchPrefix(String word) {
41+
Trie node = this;
42+
char[] words = word.toCharArray();
43+
for (int i = 0;i < words.length;i++) {
44+
node = node.next[words[i] - 'a'];
45+
if (node == null) return null;
46+
}
47+
return node;
48+
}
49+
}

0 commit comments

Comments
 (0)