Problem
Implement a trie with insert, search, and startsWith methods.
Exampleinsert("lintcode") search("code") // return false startsWith("lint") // return true startsWith("linterror") // return false insert("linterror") search("lintcode) // return true startsWith("linterror") // return trueNote
You may assume that all inputs are consist of lowercase letters a-z.
SolutionInsert. To insert a word(String) into a trie, by running every digit of the word in a for-loop, we have to first copy the TrieNode root into a new TrieNode cur like the way we treat TreeNode. Then we have to check if cur.children is null. If so, we create a new children with space of 26 for 26 characters from a to z. Then we check if the i-th children of cur referring to the i-th character of the word is null. If it’s null, put the character in that position, if it’s not, cur goes to the next. When the loop goes to the last digit of the word, set cur.exist to true.
Search. If we inserted several words in a trie, some branches, corresponding to some words, will have the exist tag being true. So, we still have to loop the word. Just like insert operation, if we see cur.children is null or cur.children[index] is null, the word does not exist. However, if the last-children.exist is true, the word exists.
Prefix. To check if a trie contains any words with a certain prefix, it’s the same as search. The only difference is you don’t need to check the extra digits after the prefix.
class TrieNode { // Initialize your data structure here. boolean exist; char ch; TrieNode[] children; public TrieNode() { } public TrieNode(char ch) { this.ch = ch; } } public class Solution { private TrieNode root; public Solution() { root = new TrieNode(); } // Inserts a word into the trie. public void insert(String word) { if (word == null || word.length() == 0) { return; } TrieNode pre = root; for (int i = 0; i < word.length(); i++) { if (pre.children == null) { pre.children = new TrieNode[26]; } int index = word.charAt(i) - "a"; if (pre.children[index] == null) { pre.children[index] = new TrieNode(word.charAt(i)); } pre = pre.children[index]; if (i == word.length() -1) { pre.exist = true; } } } // Returns if the word is in the trie. public boolean search(String word) { if (word == null || word.length() == 0) { return false; } TrieNode pre = root; for (int i = 0; i < word.length(); i++) { int index = word.charAt(i)-"a"; if (pre.children == null || pre.children[index] == null) { return false; } if (i == word.length() - 1 && pre.children[index].exist == false) { return false; } pre = pre.children[index]; } return true; } // Returns if there is any word in the trie // that starts with the given prefix. public boolean startsWith(String prefix) { if (prefix == null || prefix.length() == 0) { return false; } TrieNode pre = root; for (int i = 0; i < prefix.length(); i++) { int index = prefix.charAt(i)-"a"; if (pre.children == null || pre.children[index] == null) { return false; } pre = pre.children[index]; } return true; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/65509.html
摘要:首先,我們應(yīng)該了解字典樹(shù)的性質(zhì)和結(jié)構(gòu),就會(huì)很容易實(shí)現(xiàn)要求的三個(gè)相似的功能插入,查找,前綴查找。既然叫做字典樹(shù),它一定具有順序存放個(gè)字母的性質(zhì)。所以,在字典樹(shù)的里面,添加,和三個(gè)參數(shù)。 Problem Implement a trie with insert, search, and startsWith methods. Notice You may assume that all i...
摘要:壓縮前綴樹(shù)其實(shí)就是將所有只有一個(gè)子節(jié)點(diǎn)的節(jié)點(diǎn)合并成一個(gè),以減少?zèng)]有意義的類(lèi)似鏈表式的鏈接。然后我們開(kāi)始遍歷這個(gè)前綴樹(shù)。 Implement Trie Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs are consist of lowe...
Problem Implement a stack. You can use any data structure inside a stack except stack itself to implement it. Example push(1)pop()push(2)top() // return 2pop()isEmpty() // return truepush(3)isEmpty()...
摘要:劍指用兩個(gè)棧模擬隊(duì)列聲明文章均為本人技術(shù)筆記,轉(zhuǎn)載請(qǐng)注明出處解題思路實(shí)現(xiàn)功能用兩個(gè)棧模擬實(shí)現(xiàn)一個(gè)隊(duì)列的,和操作解題思路假設(shè)有兩個(gè)棧隊(duì)列實(shí)現(xiàn)始終用入棧實(shí)現(xiàn)隊(duì)列和實(shí)現(xiàn)由于依次出棧并壓入中,恰好保證中順序與模擬隊(duì)列順序一致,始終保證棧頂元素為模擬 劍指offer/LintCode40_用兩個(gè)棧模擬隊(duì)列 聲明 文章均為本人技術(shù)筆記,轉(zhuǎn)載請(qǐng)注明出處https://segmentfault.com...
摘要:劍指用兩個(gè)隊(duì)列實(shí)現(xiàn)一個(gè)棧聲明文章均為本人技術(shù)筆記,轉(zhuǎn)載請(qǐng)注明出處解題思路實(shí)現(xiàn)功能用兩個(gè)隊(duì)列實(shí)現(xiàn)一個(gè)棧,實(shí)現(xiàn),,和方法解題思路假設(shè)有隊(duì)列和實(shí)現(xiàn)棧的操作實(shí)現(xiàn)棧操作始終用來(lái)入隊(duì)實(shí)現(xiàn)實(shí)現(xiàn)棧的方法模擬棧的過(guò)程中,保證兩個(gè)隊(duì)列中始終有一個(gè)隊(duì)列為空,另一 劍指offer/LintCode494_用兩個(gè)隊(duì)列實(shí)現(xiàn)一個(gè)棧 聲明 文章均為本人技術(shù)筆記,轉(zhuǎn)載請(qǐng)注明出處https://segmentfault....
閱讀 3081·2021-09-24 10:26
閱讀 3236·2021-09-23 11:54
閱讀 4645·2021-09-22 15:33
閱讀 2243·2021-09-09 09:33
閱讀 1642·2021-09-07 10:10
閱讀 950·2019-08-30 11:09
閱讀 2840·2019-08-29 17:13
閱讀 993·2019-08-29 12:35