208. Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z.

總結Trie:http://www.reibang.com/p/357c1dbe31ec

Solution1:Trie

思路: Basic Trie
Time Complexity: O(word_num * word_len)
Space Complexity: O(number of nodes)

Solution1 Code:

class TrieNode {
    public TrieNode[] children = new TrieNode[26];
    public boolean isWord;
}

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

    public void insert(String word) {
        TrieNode cur = root;
        for(int i = 0; i < word.length(); i++){
            char c = word.charAt(i);
            if(cur.children[c - 'a'] == null){
                cur.children[c - 'a'] = new TrieNode();
            }
            cur = cur.children[c - 'a'];
        }
        cur.isWord = true;
    }

    public boolean search(String word) {
        TrieNode cur = root; 
        for(int i = 0; i < word.length(); i++){
            char c = word.charAt(i);
            if(cur.children[c - 'a'] == null) return false;
            cur = cur.children[c - 'a'];
        }
        return cur.isWord;
    }

    public boolean startsWith(String prefix) {
        TrieNode cur = root; 
        for(int i = 0; i < prefix.length(); i++){
            char c = prefix.charAt(i);
            if(cur.children[c - 'a'] == null) return false;
            cur = cur.children[c - 'a'];
        }
        return true;
    }
}

Solution1.Round1 Code:

class Trie {
    
    class TrieNode {
        public TrieNode[] children = new TrieNode[26];;
        public boolean is_word;
        public char val;

        public TrieNode(char c) {
            this.val = c;
        }
    }


    private TrieNode root;
    /** Initialize your data structure here. */
    public Trie() {
        root = new TrieNode('0');
    }
    
    
    /** Inserts a word into the trie. */
    public void insert(String word) {
        TrieNode cur = root;
        for(int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if(cur.children[c - 'a'] == null) {
                cur.children[c - 'a'] = new TrieNode(c);
            }
            cur = cur.children[c - 'a'];
        }
        cur.is_word = true;
    }
    
    /** Returns if the word is in the trie. */
    public boolean search(String word) {
        TrieNode cur = root;
        for(int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if(cur.children[c - 'a'] == null) {
                return false;
            }
            cur = cur.children[c - 'a'];
        }
        return cur.is_word;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
        TrieNode cur = root;
        for(int i = 0; i < prefix.length(); i++) {
            char c = prefix.charAt(i);
            if(cur.children[c - 'a'] == null) {
                return false;
            }
            cur = cur.children[c - 'a'];
        }
        return true;
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末组去,一起剝皮案震驚了整個濱河市只壳,隨后出現(xiàn)的幾起案子曾我,更是在濱河造成了極大的恐慌,老刑警劉巖腹尖,帶你破解...
    沈念sama閱讀 212,454評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異伐脖,居然都是意外死亡热幔,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,553評論 3 385
  • 文/潘曉璐 我一進店門讼庇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绎巨,“玉大人,你說我怎么就攤上這事巫俺∪纤福” “怎么了?”我有些...
    開封第一講書人閱讀 157,921評論 0 348
  • 文/不壞的土叔 我叫張陵介汹,是天一觀的道長却嗡。 經常有香客問我,道長嘹承,這世上最難降的妖魔是什么窗价? 我笑而不...
    開封第一講書人閱讀 56,648評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮叹卷,結果婚禮上撼港,老公的妹妹穿的比我還像新娘。我一直安慰自己骤竹,他們只是感情好帝牡,可當我...
    茶點故事閱讀 65,770評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蒙揣,像睡著了一般靶溜。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,950評論 1 291
  • 那天罩息,我揣著相機與錄音嗤详,去河邊找鬼。 笑死瓷炮,一個胖子當著我的面吹牛葱色,可吹牛的內容都是我干的。 我是一名探鬼主播娘香,決...
    沈念sama閱讀 39,090評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼苍狰,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了茅主?” 一聲冷哼從身側響起舞痰,我...
    開封第一講書人閱讀 37,817評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎诀姚,沒想到半個月后响牛,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 44,275評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡赫段,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,592評論 2 327
  • 正文 我和宋清朗相戀三年呀打,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片糯笙。...
    茶點故事閱讀 38,724評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡贬丛,死狀恐怖,靈堂內的尸體忽然破棺而出给涕,到底是詐尸還是另有隱情豺憔,我是刑警寧澤,帶...
    沈念sama閱讀 34,409評論 4 333
  • 正文 年R本政府宣布够庙,位于F島的核電站恭应,受9級特大地震影響,放射性物質發(fā)生泄漏耘眨。R本人自食惡果不足惜昼榛,卻給世界環(huán)境...
    茶點故事閱讀 40,052評論 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望剔难。 院中可真熱鬧胆屿,春花似錦、人聲如沸偶宫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,815評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽纯趋。三九已至彻秆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背唇兑。 一陣腳步聲響...
    開封第一講書人閱讀 32,043評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留桦锄,地道東北人扎附。 一個月前我還...
    沈念sama閱讀 46,503評論 2 361
  • 正文 我出身青樓,卻偏偏與公主長得像结耀,于是被迫代替她去往敵國和親留夜。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,627評論 2 350

推薦閱讀更多精彩內容