My code:
class TrieNode {
// Initialize your data structure here.
char c;
boolean isLeaf;
HashMap<Character, TrieNode> tracker = new HashMap<Character, TrieNode>();
public TrieNode() {
}
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
public void insert(String word) {
if (word == null || word.length() == 0)
return;
TrieNode temp = root;
for (int i = 0; i < word.length(); i++) {
char curr = word.charAt(i);
if (temp.tracker.containsKey(curr)) {
temp = temp.tracker.get(curr);
}
else {
TrieNode newNode = new TrieNode();
newNode.c = curr;
temp.tracker.put(curr, newNode);
temp = newNode;
}
if (i == word.length() - 1) {
temp.isLeaf = true;
}
}
}
// Returns if the word is in the trie.
public boolean search(String word) {
TrieNode ret = searchNode(word);
if (ret == null || !ret.isLeaf)
return false;
else
return true;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode ret = searchNode(prefix);
return ret == null ? false : true;
}
private TrieNode searchNode(String word) {
if (word == null || word.length() == 0)
return null;
TrieNode temp = root;
for (int i = 0; i < word.length(); i++) {
char curr = word.charAt(i);
if (!temp.tracker.containsKey(curr)) {
return null;
}
else {
temp = temp.tracker.get(curr);
}
}
return temp;
}
}
// Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");
之前写过trie。今天为了节省时间,直接看了答案再自己重写了一遍。
http://www.programcreek.com/2014/05/leetcode-implement-trie-prefix-tree-java/
Anyway, Good luck, Richardo!
My code:
class TrieNode {
// Initialize your data structure here.
TrieNode[] next;
char val;
boolean isWord;
public TrieNode() {
next = 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) {
insert(word, 0, root);
}
private void insert(String word, int index, TrieNode root) {
char curr = word.charAt(index);
TrieNode nextNode = null;
if (root.next[curr -'a'] != null) {
nextNode = root.next[curr - 'a'];
}
else {
nextNode = new TrieNode();
root.next[curr - 'a'] = nextNode;
}
if (index == word.length() - 1) {
nextNode.isWord = true;
return;
}
insert(word, index + 1, nextNode);
}
// Returns if the word is in the trie.
public boolean search(String word) {
TrieNode next = root;
for (int i = 0; i < word.length(); i++) {
char curr = word.charAt(i);
if (next.next[curr - 'a'] == null) {
return false;
}
else {
next = next.next[curr - 'a'];
}
}
return next.isWord;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode next = root;
for (int i = 0; i < prefix.length(); i++) {
char curr = prefix.charAt(i);
if (next.next[curr - 'a'] == null) {
return false;
}
else {
next = next.next[curr - 'a'];
}
}
return true;
}
}
// Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");
差不多的思路吧。
reference:
https://leetcode.com/articles/implement-trie-prefix-tree/
Anyway, Good luck, Richardo! -- 09/11/2016