Skip to content

Commit e52d1f6

Browse files
committed
1. 新增另一种解决方法
1 parent ecf122f commit e52d1f6

2 files changed

Lines changed: 145 additions & 1 deletion

File tree

‎java-note-algorithm/src/main/java/com/leosanqing/leetcode/medium/tree/_208_implementTrie.java‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static void main(String[] args) {
4040
}
4141

4242
class TrieNode{
43-
private final Map<Character,TrieNode> children = new HashMap<>();
43+
private final Map<Character, TrieNode> children = new HashMap<>();
4444

4545
public boolean isEnd = false;
4646

@@ -99,3 +99,64 @@ public boolean startsWith(String prefix) {
9999
return true;
100100
}
101101
}
102+
103+
104+
/**
105+
* 使用 数组实现
106+
*/
107+
class Trie2 {
108+
109+
110+
TrieNode2 root = new TrieNode2();
111+
112+
/** Initialize your data structure here. */
113+
public Trie2() {
114+
115+
}
116+
117+
/** Inserts a word into the trie. */
118+
public void insert(String word) {
119+
TrieNode2 curr = root;
120+
for (char ch : word.toCharArray()) {
121+
if(curr.children[ch - 'a'] != null){
122+
curr = curr.children[ch - 'a'];
123+
}else{
124+
curr.children[ch - 'a'] = new TrieNode2();
125+
}
126+
}
127+
curr.isEnd = true;
128+
}
129+
130+
/** Returns if the word is in the trie. */
131+
public boolean search(String word) {
132+
TrieNode2 curr = root;
133+
for (char ch : word.toCharArray()) {
134+
curr = curr.children[ch - 'a'];
135+
if (curr == null) {
136+
return false;
137+
}
138+
}
139+
return curr.isEnd;
140+
}
141+
142+
/** Returns if there is any word in the trie that starts with the given prefix. */
143+
public boolean startsWith(String prefix) {
144+
TrieNode2 curr = root;
145+
for (char c : prefix.toCharArray()) {
146+
curr = curr.children[c - 'a'];
147+
if (curr == null) {
148+
return false;
149+
}
150+
}
151+
return true;
152+
}
153+
}
154+
155+
class TrieNode2{
156+
/** 因为有26个字符 */
157+
public TrieNode2[] children = new TrieNode2[26];
158+
159+
public boolean isEnd = false;
160+
161+
162+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.leosanqing.leetcode.medium.tree;
2+
3+
/**
4+
* @Author: rtliu
5+
* @Date: 2020/6/28 下午3:58
6+
* @Package: com.leosanqing.leetcode.medium.tree
7+
* @Description: Design a data structure that supports the following two operations:
8+
* void addWord(word) bool search(word) search(word) can search a literal word
9+
* or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.
10+
* Example:
11+
* addWord("bad")
12+
* addWord("dad")
13+
* addWord("mad")
14+
* search("pad") -> false
15+
* search("bad") -> true
16+
* search(".ad") -> true
17+
* search("b..") -> true
18+
* @Version: 1.0
19+
*/
20+
public class _211_addAndSearchWord_dataStructureDesign {
21+
22+
}
23+
24+
class WordDictionary {
25+
TrieNode1 root = new TrieNode1();
26+
27+
/** Initialize your data structure here. */
28+
public WordDictionary() {
29+
30+
}
31+
32+
/** Adds a word into the data structure. */
33+
public void addWord(String word) {
34+
TrieNode1 curr = root;
35+
if(word != null && word.length() > 0){
36+
37+
for (char c : word.toCharArray()) {
38+
if (curr.children[c - 'a'] == null) {
39+
curr.children[c - 'a'] = new TrieNode1();
40+
}
41+
curr = curr.children[c -'a'];
42+
43+
}
44+
curr.isEnd = true;
45+
46+
}
47+
}
48+
49+
/** Returns if the word is in the data structure.
50+
* A word could contain the dot character '.' to represent any one letter. */
51+
public boolean search(String word) {
52+
return helper(word, 0 , root);
53+
}
54+
55+
56+
private boolean helper(String word, int index, TrieNode1 curr){
57+
if(index >= word.length()){
58+
return curr.isEnd;
59+
}
60+
char c = word.charAt(index);
61+
62+
if('.' == c){
63+
for (int i = 0; i < curr.children.length; i++) {
64+
// 当出现 . 的时候,递归遍历
65+
if(curr.children[i] != null && helper(word, index+1, curr.children[i])){
66+
return true;
67+
}
68+
}
69+
return false;
70+
}else {
71+
return (curr.children[c - 'a'] != null) && helper(word,index +1, curr.children[c - 'a']);
72+
}
73+
}
74+
}
75+
76+
class TrieNode1 {
77+
TrieNode1[] children = new TrieNode1[26];
78+
79+
boolean isEnd = false;
80+
81+
}
82+
83+

0 commit comments

Comments
 (0)