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. Oracle Form Builder

    Oracle Form Builder 是Oracle的一个开发工具,可以针对Oracle公司的E-Business Suit的ERP系统开发的.对应的还有reports builder. Oracl ...

  2. Prim's Algorithm & Kruskal's algorithm

    1. Problem These two algorithm are all used to find a minimum spanning tree for a weighted undirecte ...

  3. PythonWeb 服务部署文档及迁移到Linux相关

    pythonWeb的部署(Django+Uwsgi): 1. 部署服务器上需要的Python3.6环境: 安装集成了python3.6 和pip ,virtualenv虚拟环境 的Anaconda(A ...

  4. C/C++ 打印文件名、行号、函数名的方法

    转自:http://zhidao.baidu.com/link?url=JLCaxBAXLJVcx_8jsyJVF92E_bZjo4ONJ5Ab-HGlNBc1dfzcAyFAIygwP1qr18aa ...

  5. Vue2.0组件之间通信(转载)

    Vue中组件这个特性让不少前端er非常喜欢,我自己也是其中之一,它让前端的组件式开发更加合理和简单.笔者之前有写过一篇Vue2.0子父组件通信,这次我们就来聊一聊平级组件之间的通信. 首先我们先搭好开 ...

  6. apache +PHP多版本 切换的问题

    在开发中切换php版本的时候出错 经过2小时的日子排查终于找到是因为切换版本后加载的php7ts.dll模块还是原来版本的,因此保pid file 错误 解决方法 PHPIniDir "H: ...

  7. Js 中的原始值和引用值

    最近遇写 node.js 时到一个问题,把对象当赋值给数组成员时总是出错,比如下面的代码, var Arr = new Array(); var Obj = new Object(); for(var ...

  8. Kafka日志存储原理

    引言 Kafka中的Message是以topic为基本单位组织的,不同的topic之间是相互独立的.每个topic又可以分成几个不同的partition(每个topic有几个partition是在创建 ...

  9. BZOJ4919 [Lydsy1706月赛]大根堆 【dp + 启发式合并】

    题目链接 BZOJ4919 题解 链上的\(LIS\)维护一个数组\(f[i]\)表示长度为\(i\)的\(LIS\)最小的结尾大小 我们可以用\(multiset\)来维护这个数组,子树互不影响,启 ...

  10. 洛谷 P1053 逛公园 解题报告

    P3953 逛公园 问题描述 策策同学特别喜欢逛公园. 公园可以看成一张\(N\)个点\(M\)条边构成的有向图,且没有自环和重边.其中1号点是公园的入口,\(N\)号点是公园的出口,每条边有一个非负 ...