Description:

Implement a trie with insertsearch, and startsWith methods.

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

实现一个简单的Trie树,参考我的博客:http://www.cnblogs.com/wxisme/p/4876197.html

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

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

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

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

  2. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

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

  3. [LeetCode] Implement Trie (Prefix Tree)

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

  4. (medium)LeetCode .Implement Trie (Prefix Tree)

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

  5. LeetCode Implement Trie (Prefix Tree) (实现trie树3个函数:插入,查找,前缀)

    题意:实现trie树的3个功能,只含小写字母的串. 思路:老实做即可! class TrieNode { public: TrieNode* chd[]; bool flag; // Initiali ...

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

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

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

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

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

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

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

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

随机推荐

  1. 由“如何取得CPU的温度与型号”学到的知识延伸WQL

    [Base]:WMI是一项核心的 Windows 管理技术:用户可以使用 WMI 管理本地和远程计算机.WQL就是 WMI 中的查询语言,翻译成中文好像可以成为 Windows 管理规范查询语言. 1 ...

  2. 制作U盘启动安装CentOS Linux系统

    制作U盘启动安装CentOS Linux系统 (特为老男孩教育&&51CTO学院在线三期同学而发) 方法一:使用UltraISO,将u盘做成启动盘 文件-->打开-->选择 ...

  3. js 模拟鼠标事件

    <!DOCTYPE html> <html> <head lang="zh-CN"> <meta charset="UTF-8& ...

  4. T4生成多文件时,不生成自己

    如:我用的网上的生成多文件的一个include文件. 生成多文件时,默认会生成一个以自己名字命名的文件如: 有一个demo.tt文件,生成时会出来一个demo.cs文件(默认情况下) 解决方法: Fo ...

  5. java注解自定义使用

    Java提供了4种注解,专门负责新注解的创建: @Target: 表示该注解可以用于什么地方,可能的ElementType参数有:CONSTRUCTOR:构造器的声明FIELD:域声明(包括enum实 ...

  6. 关于form表单onsubmi提交

    表单允许客户端的用户以标准格式向服务器提交数据.表单的创建者为了收集所需数据,使用了各种控件设计表单如 INPUT 或 SELECT.查看表单的用户只需填充数据并单击提交按钮即可向服务器发送数据.服务 ...

  7. Unity3d游戏开发中使用可空类型(Nullable Types)

    你怎么确定一个Vector3,int,或float变量是否被分配了一个值?一个方便的方式就是使用可空类型! 有时变量携带重要信息,但仅仅有在特定的游戏事件发生时触发.比如:一个角色在你的游戏可能闲置, ...

  8. JUC回顾之-ArrayBlockingQueue底层实现和原理

    ArrayBlockingQueue的原理和底层实现的数据结构 : ArrayBlockingQueue是数组实现的线程安全的有界的阻塞队列,可以按照 FIFO(先进先出)原则对元素进行排序. 线程安 ...

  9. django 配置中STATICFILES_DIRS 和STATIC_ROOT不能同时出现

    系统环境: win7 django版本查看: 启动django项目的时候,一直找不到静态资源,很奇怪放在linux服务器上的时候好好的,拿下来随便修改了配置就说url找不到了. 用wingIDE没有任 ...

  10. Python--异常处理--12

    Python 异常处理 原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ python提供了两个非常重要的功能来处理python程序在运行中出现的 ...