0%

Trie树的实现及应用

Trie树的实现

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
class TrieNode {
// Initialize your data structure here.
boolean isLeaf;
TrieNode[] nodes;
public TrieNode() {
isLeaf = false;
nodes = new TrieNode[26];
}
}

public class Trie {
private TrieNode root;

public Trie() {
root = new TrieNode();
}

// Inserts a word into the trie.
public void insert(String word) {
TrieNode node = root;
for(int i = 0; i < word.length(); ++i) {
int index = word.charAt(i) - 'a';
/***错误的写法,node信息丢失
* node = node.nodes[index];
* if(node == null)
* node = new TrieNode();
***/
if(node.nodes[index] == null)
node.nodes[index] = new TrieNode();
node = node.nodes[index];
if(i == word.length() - 1)
node.isLeaf = true;
}
}

// Returns if the word is in the trie.
public boolean search(String word) {
TrieNode node = root;
for(int i = 0; i < word.length(); ++i) {
int index = word.charAt(i) - 'a';
node = node.nodes[index];
if(node == null)
return false;
if(i == word.length() - 1 && node.isLeaf)
return true;
}
return false;
}

// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String word) {
TrieNode node = root;
for(int i = 0; i < word.length(); ++i) {
int index = word.charAt(i) - 'a';
node = node.nodes[index];
if(node == null)
return false;
}
return true;
}
}

// Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");

Word Search II

Given a 2D board and a list of words from the dictionary, find all words in the board.

Each word must be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

For example,
Given words = [“oath”,”pea”,”eat”,”rain”] and board =

[
[‘o’,’a’,’a’,’n’],
[‘e’,’t’,’a’,’e’],
[‘i’,’h’,’k’,’r’],
[‘i’,’f’,’l’,’v’]
]
Return [“eat”,”oath”].
Note:
You may assume that all inputs are consist of lowercase letters a-z.
https://leetcode.com/problems/word-search-ii/

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
class Trie {
private class TrieNode {
boolean isLeaf;
TrieNode[] nodes;
public TrieNode() {
isLeaf = false;
nodes = new TrieNode[26];
}
}

private TrieNode root;
public Trie() {
root = new TrieNode();
}

public void insert(String word) {
if(word == null || word.length() == 0) return;
TrieNode node = root;
for(int i = 0; i < word.length(); ++i) {
char ch = word.charAt(i);
if(node.nodes[ch - 'a'] == null)
node.nodes[ch - 'a'] = new TrieNode();
node = node.nodes[ch - 'a'];
if(i == word.length() - 1)
node.isLeaf = true;
}
}

public boolean search(String word) {
TrieNode node = root;
for(int i = 0; i < word.length(); ++i) {
int index = word.charAt(i) - 'a';
if(node.nodes[index] == null)
return false;
node = node.nodes[index];
if(i == word.length() - 1 && node.isLeaf)
return true;
}
return false;
}

public boolean startWith(String word) {
TrieNode node = root;
for(int i = 0; i < word.length(); ++i) {
int index = word.charAt(i) - 'a';
if(node.nodes[index] == null)
return false;
node = node.nodes[index];
}
return true;
}
}

public class Solution {
public List<String> findWords(char[][] board, String[] words) {
List<String> result = new ArrayList<String>();
if(board == null || board.length == 0 || words == null) return result;
Trie dict = new Trie();
for(String word : words)
dict.insert(word);
for(int i = 0; i < board.length; ++i) {
for(int j = 0; j < board[0].length; ++j) {
dfs(board, i, j, dict, result, new StringBuilder());
}
}
return result;
}

private void dfs(char[][] board, int x, int y, Trie dict, List<String> result, StringBuilder s) {
if(x < 0 || x >= board.length || y < 0 || y >= board[0].length || board[x][y] == '#') return;
if(!dict.startWith(s.toString())) return;
char tmp = board[x][y];
s.append(tmp);
String cur = s.toString();
if(dict.search(cur) && !result.contains(cur))
result.add(cur);
board[x][y] = '#';
dfs(board, x + 1, y, dict, result, s);
dfs(board, x - 1, y, dict, result, s);
dfs(board, x, y - 1, dict, result, s);
dfs(board, x, y + 1, dict, result, s);
board[x][y] = tmp;
s.deleteCharAt(s.length() - 1);
}
}