-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path211.添加与搜索单词-数据结构设计.java
78 lines (64 loc) · 1.81 KB
/
211.添加与搜索单词-数据结构设计.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
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/*
* @lc app=leetcode.cn id=211 lang=java
*
* [211] 添加与搜索单词 - 数据结构设计
*/
// @lc code=start
class WordDictionary {
static class Node implements Comparable<Node>{
Character val;
Boolean end = false;
Map<Character, Node> map;
Node(Character val){
this.val = val;
map = new HashMap<>();
}
@Override
public int compareTo(Node node) {
return val - node.val;
}
}
Node root = new Node(null);
public WordDictionary() {
}
public void addWord(String word) {
Node cur = root;
int i;
for(i = 0;i < word.length();++i){
char cc = word.charAt(i);
if(! cur.map.containsKey(cc)){
cur.map.put(cc, new Node(cc));
}
cur = cur.map.get(cc);
}
cur.end = true;
}
public boolean search(String word) {
return dfs(root, word, 0);
}
private boolean dfs(Node curNode, String word, int index){
if(index == word.length()){
return curNode.end;
}
char cc = word.charAt(index);
if(curNode.map.size() == 0) return false;
if(cc == '.'){
for(Map.Entry<Character, Node> e : curNode.map.entrySet()){
if(dfs(e.getValue(), word, index + 1)) return true;
}
return false;
}
if(!curNode.map.containsKey(cc)) return false;
return dfs(curNode.map.get(cc), word, index + 1);
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/
// @lc code=end