Leetcode - Add and Search Word - Data structure design

Screenshot from 2016-03-02 00:15:09.png

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

http://stackoverflow.com/questions/12069877/what-the-iteration-cost-on-a-hashset-also-depend-on-the-capacity-of-backing-map

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容