My code:
public class WordDictionary {
private class TrieNode {
char c;
boolean isLeaf;
HashMap<Character, TrieNode> tracker = new HashMap<Character, TrieNode>();
TrieNode() {
}
}
private TrieNode root = new TrieNode();
// Adds a word into the data structure.
public void addWord(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 data structure. A word could
// contain the dot character '.' to represent any one letter.
public boolean search(String word) {
if (word == null || word.length() == 0)
return false;
return search(word, 0, root);
}
private boolean search(String word, int p, TrieNode t) {
if (p >= word.length())
return t.isLeaf;
TrieNode temp = t;
boolean ret = false;
for (int i = p; i < word.length(); i++) {
char curr = word.charAt(i);
if (curr == '.') {
for (Character c : temp.tracker.keySet()) {
TrieNode next = temp.tracker.get(c);
ret = ret | search(word,i + 1, next);
if (ret)
return ret;
}
return false;
}
else {
if (!temp.tracker.containsKey(curr))
return false;
else {
temp = temp.tracker.get(curr);
}
}
}
return temp.isLeaf;
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
自己寫了出來(lái)。
主要一個(gè)地方是黄绩,
"." 的時(shí)候羡洁,需要多個(gè)入口dfs,那么爽丹,只要有一個(gè)dfs成功了筑煮,后面的就不需要繼續(xù)進(jìn)行了,直接return粤蝎。這樣才能不超時(shí)真仲。
Anyway, Good luck, Richardo!
My code:
public class WordDictionary {
private TrieNode root = new TrieNode('-');
private class TrieNode {
boolean isWord = false;
HashMap<Character, TrieNode> table;
char val;
TrieNode(char input) {
val = input;
table = new HashMap<Character, TrieNode>();
}
}
// Adds a word into the data structure.
public void addWord(String word) {
if (word == null || word.length() == 0) {
return;
}
TrieNode node = root;
for (int i = 0; i < word.length(); i++) {
char curr = word.charAt(i);
if (node.table.containsKey(curr)) {
node = node.table.get(curr);
}
else {
TrieNode newTrieNode = new TrieNode(curr);
node.table.put(curr, newTrieNode);
node = newTrieNode;
}
}
node.isWord = true;
}
// Returns if the word is in the data structure. A word could
// contain the dot character '.' to represent any one letter.
public boolean search(String word) {
if (word == null || word.length() == 0) {
return false;
}
return search(word, root, 0);
}
private boolean search(String word, TrieNode node, int index) {
if (index >= word.length()) {
return node.isWord;
}
char curr = word.charAt(index);
if (node.table.containsKey(curr)) {
node = node.table.get(curr);
return search(word, node, index + 1);
}
else if (curr == '.') {
boolean flag = false;
Set<Character> set = node.table.keySet();
for (Character c : set) {
flag = flag | search(word, node.table.get(c), index + 1);
if (flag) {
return true;
}
}
return false;
}
else {
return false;
}
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
這道題目的思想和以前差不多。一開(kāi)始沒(méi)做出來(lái)初澎。
另外可以用 array來(lái)代替 hashmap
另外:
java hashset iterator performance
hashmap .keySet() O(1)
for (Character c : set) {...} O(n) n = backup array length
遍歷的時(shí)候秸应,仍然要遍歷整個(gè)數(shù)組,如果為空碑宴,就跳過(guò)软啼。
Anyway, Good luck, Richardo! -- 08/19/2016