Implement a trie with insertsearch, and startsWith methods.

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

解法:

  Trie(字典树)的知识参见:数据结构之Trie树 和 [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

  可以采用数组和哈希表的方式实现,代码分别如下:

public class Trie {
private TrieNode root; /** Initialize your data structure here. */
public Trie() {
root = new TrieNode();
} /** Inserts a word into the trie. */
public void insert(String word) {
root.insert(word, 0);
} /** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode result = root.search(word, 0);
return result != null && result.getIsWord();
} /** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode result = root.search(prefix, 0);
return result != null;
}
} class TrieNode {
private TrieNode[] children;
private boolean isWord; public TrieNode() {
children = new TrieNode[26];
isWord = false;
} public void insert(String word, int index) {
// 如果所有字符都已插入,需要将最后一个字符节点的isWord改为true
if (index == word.length()) {
this.isWord = true;
return;
}
// 如果不存在该字符,在对应位置新建节点
int n = word.charAt(index) - 'a';
if (children[n] == null) {
children[n] = new TrieNode();
}
// 继续下一字符
children[n].insert(word, index + 1);
} // 由于Trie中既要求实现search,又要求实现startsWith,为了方便,
// 此处直接返回搜索结果的TrieNode,交由Trie去判断。
public TrieNode search(String word, int index) {
if (index == word.length()) {
return this;
}
int n = word.charAt(index) - 'a';
if (children[n] == null) {
return null;
}
return children[n].search(word, index + 1);
} public boolean getIsWord() {
return this.isWord;
}
} /**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/
public class Trie {
private TrieNode root; /** Initialize your data structure here. */
public Trie() {
root = new TrieNode();
} /** Inserts a word into the trie. */
public void insert(String word) {
TrieNode curr = root;
for (int i = 0; i < word.length(); i++) {
char letter = word.charAt(i);
if (!curr.children.containsKey(letter)) {
curr.children.put(letter, new TrieNode());
}
curr = curr.children.get(letter);
}
curr.isWord = true;
} /** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode result = find(word);
return result != null && result.isWord;
} /** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode result = find(prefix);
return result != null;
} public TrieNode find(String word) {
TrieNode curr = root;
for (int i = 0; i < word.length(); i++) {
char letter = word.charAt(i);
if (!curr.children.containsKey(letter)) {
return null;
}
curr = curr.children.get(letter);
}
return curr;
}
} class TrieNode {
HashMap<Character, TrieNode> children;
boolean isWord; public TrieNode() {
children = new HashMap<>();
isWord = false;
}
} /**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/

[LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆的更多相关文章

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

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

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

    Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...

  3. Java for LeetCode 208 Implement Trie (Prefix Tree)

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

  4. leetcode@ [208] Implement Trie (Prefix Tree)

    Trie 树模板 https://leetcode.com/problems/implement-trie-prefix-tree/ class TrieNode { public: char var ...

  5. LeetCode 208 Implement Trie (Prefix Tree) 字典树(前缀树)

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

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

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

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

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

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

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

  9. 【leetcode】208. Implement Trie (Prefix Tree 字典树)

    A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...

随机推荐

  1. 《大象Think in UML》阅读笔记之一

    Think in UML这一书以UML为载体,将面向对象的分析设计思想巧妙地融合在建模UML当中,通过一些实例将软件系统的开发过程中的一些知识有机地结合起来.全书共分为四篇:准备篇.基础篇.进阶篇和总 ...

  2. asp.net如何隐藏表格(table)的一行

    直接用jquery $("#id1").click(function(){ $("#trId").css("display""no ...

  3. AG-Admin微服务框架入门

    AG-Admin微服务框架入门  @qq群:一群: 837736451  二群 169824183 一 概要介绍 AG-Admin后台地址:https://gitee.com/minull/ace-s ...

  4. JavaScript下的new操作符做了什么?

    可以参考知乎的一篇文章:https://zhuanlan.zhihu.com/p/23987456 参考网上其他人的文章,new发生了以下操作 参考MDN:https://developer.mozi ...

  5. golang build 的简单用法.(菜鸟初学)

    1. golang 里面的 go build 可以编译代码. go build helloworld.go 2. 这里面有一个注意事项事项. 如果引用非go语言的 内置package的话 需要在环境变 ...

  6. JavaScript 稀奇的js语法

    function c(expression) { console.log(expression); } c(-0); // -0 c(-0 === +0); // true c((-0).toStri ...

  7. SpannableString的基本用法

    原文地址:http://www.cnblogs.com/kross/p/3645594.html 以前一直好奇QQ的输入框里面是如何出现表情的,今天看了下这个,心中发出“原来是这样啊”的感叹. 通常情 ...

  8. ACM数论之旅9---中国剩余定理(CRT)(壮哉我大中华╰(*°▽°*)╯)

    中国剩余定理,又名孙子定理o(*≧▽≦)ツ 能求解什么问题呢? 问题: 一堆物品 3个3个分剩2个 5个5个分剩3个 7个7个分剩2个 问这个物品有多少个 解这题,我们需要构造一个答案 我们需要构造这 ...

  9. Spring学习13-中IOC(工厂模式)和AOP(代理模式)的详细解释

    我们是在使用Spring框架的过程中,其实就是为了使用IOC,依赖注入,和AOP,面向切面编程,这两个是Spring的灵魂. 主要用到的设计模式有工厂模式和代理模式. IOC是工厂模式参考:设计模式- ...

  10. Rabbit and Grass HDU - 1849 (Bash+Nim)

    就是Bash 和 Nim 博弈的结合  可以直接 res ^= (Li + 1) % Mi 也可以 sg打个表  我打了个表 #include <iostream> #include &l ...