Implement a trie with insertsearch, and startsWith methods.

Note:
You may assume that all inputs are consist of lowercase letters a-z.

实现一个字典树。

好久不做题,没感觉啊,TreeNode用一个布尔变量表示是否是一个合法单词的结尾即可,一开始还用cnt来计数,search的时候比较麻烦。

class TrieNode {
// Initialize your data structure here.
TrieNode [] next;
boolean valid;
public TrieNode() {
next = new TrieNode[26];
valid=false;
}
} public class Trie {
private TrieNode root; public Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
public void insert(String word) {
TrieNode ptr = root;
for(char c:word.toCharArray()){
if(ptr.next[c-'a']==null){
ptr.next[c-'a'] = new TrieNode();
}
ptr=ptr.next[c-'a'];
}
ptr.valid=true;
} // Returns if the word is in the trie.
public boolean search(String word) {
int last = 0;
TrieNode ptr = root;
for(char c:word.toCharArray()){
if(ptr==null||ptr.next[c-'a']==null){
return false;
}
ptr=ptr.next[c-'a'];
}
return ptr.valid;
} // Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode ptr = root;
for(char c:prefix.toCharArray()){
if(ptr==null||ptr.next[c-'a']==null){
return false;
}
ptr=ptr.next[c-'a'];
}
return true;
}
} // Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");

Implement Trie (Prefix Tree) ——LeetCode的更多相关文章

  1. Implement Trie (Prefix Tree) - LeetCode

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  2. leetcode面试准备:Implement Trie (Prefix Tree)

    leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...

  3. [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  4. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  5. 【LeetCode】208. Implement Trie (Prefix Tree)

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...

  6. 【刷题-LeetCode】208. Implement Trie (Prefix Tree)

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Example: ...

  7. LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

    字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith  ...

  8. 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...

  9. Leetcode: Implement Trie (Prefix Tree) && Summary: Trie

    Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...

随机推荐

  1. 与数据库打交道的Adapter----SimpleCursorAdapter

    http://www.cnblogs.com/wenjiang/p/3196486.html 程序员是这个世界上最神奇的职业,因为几乎所有其他职业的人都能转到该行来,只要他智力正常,有接受过正规的编程 ...

  2. django连接已有的数据库

    以连接postgresql为例: 1.安装psycopg2,下载地址:http://www.stickpeople.com/projects/python/win-psycopg/ 2.配置setti ...

  3. SQL数据库安装

    安装过程中经常出现失败或者提示,那么久要清楚干净所有的数据在重新安装,步骤如下. SQL2008卸载 一.从控制面板卸载 1)点击计算机右下角“开始”,点击“控制面板” 2)点击“卸载程序”. 卸载与 ...

  4. C# 通过hessian调Java注意事项

    照理说C#可以通过标准的web服务可以轻松地调用Java,但是鉴于hessian的高性能及开发效率,个人认为C#通过hessian调用java是很值得提倡的.之前完成的一个比较大型的企业应用项目就是采 ...

  5. 【转】 C++库常用函数一览

    本文中提到的函数库有:<string> <cctype> <algorithm> <cmath> <cstdlib> <iomanip ...

  6. MATLAB中mexFunction函数的接口规范

    MEX文件的调用极为方便,其调用方式与MATALAB的内建函数完全相同,只需要在命令窗口内输入对应的文件名称即可. C语言MEX程序代码文件有计算子例程(Computational routine)和 ...

  7. SGU 130.Circle

    答案为Catalan数C(2k, k)/(k+1) #include <stdio.h> using namespace std; int k; int main() { scanf(&q ...

  8. MYSQL根据分类分组取每组一条数据且按条件能排序的写法

    之前在一个项目的开发中,有遇到要根据分类来分组获取每组一条按某个条件字段排序的数据结果,于是先自己写了一条语句: select * from `表A` GROUP BY `c`; 上面这个语句有可以根 ...

  9. C++最后课程项目总结

    第一次独立完成的C++小项目,40小时 + 5小时Update + 8小时Linux移植. 过程: 过程非常认真,一个星期主要就是忙这个,为了完成某个部分,有时饭都推迟吃,连续对着电脑10几个小时很累 ...

  10. sql语句复制表

    1.复制表结构及数据到新表CREATE TABLE 新表 SELECT * FROM 旧表 这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete from newtable; ...