(medium)LeetCode .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
.
思路:实现单词查找树,它是一种树形结构;用于保存大量的字符串。它的优点是:利用字符串的公共前缀来节约存储空间。每个节点有26个从节点。isEndOfWord,children,两个关键变量。
代码如下:
class TrieNode {
// Initialize your data structure here.
boolean isEndOfWord;
TrieNode[] children;
public TrieNode() {
this.isEndOfWord=false;
this.children=new TrieNode[26];
} } public class Trie {
private TrieNode root; public Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
public void insert(String word) {
TrieNode runner=root;
for(char c:word.toCharArray()){
if(runner.children[c-'a']==null){
runner.children[c-'a']=new TrieNode();
}
runner=runner.children[c-'a'];
}
runner.isEndOfWord=true;
} // Returns if the word is in the trie.
public boolean search(String word) {
TrieNode runner=root;
for(char c:word.toCharArray()){
if(runner.children[c-'a']==null){
return false;
}else{
runner=runner.children[c-'a'];
}
}
return runner.isEndOfWord;
} // Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode runner=root;
for(char c:prefix.toCharArray()){
if(runner.children[c-'a']==null){
return false;
}else{
runner=runner.children[c-'a'];
}
}
return true;
}
} // Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");
运行结果:
(medium)LeetCode .Implement Trie (Prefix Tree)的更多相关文章
- Leetcode: Implement Trie (Prefix Tree) && Summary: Trie
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- [LeetCode] Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- LeetCode——Implement Trie (Prefix Tree)
Description: Implement a trie with insert, search, and startsWith methods. Note:You may assume that ...
- LeetCode Implement Trie (Prefix Tree) (实现trie树3个函数:插入,查找,前缀)
题意:实现trie树的3个功能,只含小写字母的串. 思路:老实做即可! class TrieNode { public: TrieNode* chd[]; bool flag; // Initiali ...
- [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- 【LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
随机推荐
- .net 添加Cookie的4种方法
第一种添加Cookie方法 HttpCookie myCookie = new HttpCookie("userrole"); myCookie.Values["a&qu ...
- 解决 nginx https反向代理http协议 302重定向localtion到http问题
location /rest { #proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remot ...
- 打印Java main方法执行的命令参数代码
RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean(); List<String> jvmArgs = bean.getInpu ...
- 给Jquery动态添加的元素添加事件2
$('div').on('click', 'button', function(e){console.log($(e.target).is(':button'))}); $('<button&g ...
- Debian安装记录
Fedora着实让我伤心透了.前天和昨天搞了整整两天Fedora 20的安装,午睡也没有,晚上就睡了四个小时不到,几乎尝试了所有Fedora 20的桌面版本,全部出问题了!就因为我的笔记本显卡是ATI ...
- sql访注入
http://www.dewen.org/q/6154/java%E7%A8%8B%E5%BA%8F%E9%98%B2%E6%AD%A2sql%E6%B3%A8%E5%85%A5%E7%9A%84%E ...
- 真实赛车3,SPEEDRUSH TV 第3季,第3阶段(第3天),直线加速赛
与其跳过,不如金币升级引擎和车身.因为后边紧跟一场计时赛.
- 236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- item31,连续子数组的最大和
整型数组,元素有正数和负数.数组中一个或连续的多个整数组成一个子数组,求所有子数组中最大值. =========== 动态规划, 状态转移方程,max[].size = nums.size() max ...
- Skip StyleCop Warnings.
[SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMust ...