1. 程式人生 > 實用技巧 >字首和 字首樹

字首和 字首樹

實現 Trie (字首樹)

class Trie {

    class TireNode {
        boolean isEnd = false;
        TireNode[] next = new TireNode[26];
        TireNode() {} 
    }

    private TireNode root;

    /** Initialize your data structure here. */
    public Trie() {
        root = new TireNode();
    }
    
    /** Inserts a word into the trie. 
*/ public void insert(String word) { TireNode node = root; for (char ch : word.toCharArray()) { if (node.next[ch-'a'] == null) { node.next[ch-'a'] = new TireNode(); } node = node.next[ch-'a']; } node.isEnd = true; }
/** Returns if the word is in the trie. */ public boolean search(String word) { TireNode node = root; for (char ch : word.toCharArray()) { if (node.next[ch-'a'] == null) return false; node = node.next[ch-'a']; } return node.isEnd; }
/** Returns if there is any word in the trie that starts with the given prefix. */ public boolean startsWith(String prefix) { TireNode node = root; for (char ch : prefix.toCharArray()) { if (node.next[ch-'a'] == null) return false; node = node.next[ch-'a']; } return true; } } /** * Your Trie object will be instantiated and called as such: * Trie obj = new Trie(); * obj.insert(word); * boolean param_2 = obj.search(word); * boolean param_3 = obj.startsWith(prefix); */

新增與搜尋單詞 - 資料結構設計

回溯那裡多注意看看,因為過於依賴之前解數獨的寫法,犯了一個錯誤,即每一層都應該是true或者false才是。在迴圈中要用到if(flashback()) return true,但是迴圈一結束立馬要return fasle。因為還有沒有找到的情況吶。這是針對迴圈的特定寫法。如果不是迴圈的話,直接是return (flashback())。

class WordDictionary {

    class TireNode {
        boolean isEnd = false;
        TireNode[] next = new TireNode[26];
        TireNode() {}
    }

    private TireNode root; 

    /** Initialize your data structure here. */
    public WordDictionary() {
        root = new TireNode();
    }
    
    /** Adds a word into the data structure. */
    public void addWord(String word) {
        TireNode node =  root;
        for (char ch : word.toCharArray()) {
            if (node.next[ch-'a'] == null) {
                node.next[ch-'a'] = new TireNode();
            }
            node = node.next[ch-'a'];
        }
        node.isEnd = 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) {
        return searchHelp(word.toCharArray(), 0, root);
    }

    private boolean searchHelp(char[] word, int index, TireNode node) {
        if (node == null) return false;
        for (int i = index; i < word.length; i++) {
            char ch = word[index];
            if (ch != '.') {
                //這裡就是直接到下一層了。
                //開始寫成
                //if (searchHelp(word, index + 1, node.next[ch-'a'])) return true;
                //但是這樣不行,因為還要加上 else return false; 才算完整。
                return (searchHelp(word, index + 1, node.next[ch-'a']);
            } else {
                //這裡我悟了,每次都要得到false或者true,因為在找不到匹配就false,找到就true。
                for (int j = 0; j < 26; j++) {
                    if (searchHelp(word, index + 1, node.next[j])) return true;
                }
                return false;
            }
        }
        return node.isEnd;
    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * boolean param_2 = obj.search(word);
 */