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. C++编译与链接(1)-编译与链接过程

    大家知道计算机使用的一系列的1和0 那个一个C++语言程序又是如何从一个个.h和.cpp文件变成包含1和0的可执行文件呢? 可以认为有以下的几个环节 源程序->预处理->编译和优化-> ...

  2. C++操作mysql方法总结(2)

    C++通过ODBC和通过MFC ODBC操作mysql的两种方式 使用vs2013和64位的msql 5.6.16进行操作 项目中使用的数据库名和表数据请参考C++操作mysql方法总结(1)中的介绍 ...

  3. Windows10(UWP)下的MEF

    前言 最近在帮一家知名外企开发Universal Windows Platform的相关应用,开发过程中不由感慨:项目分为两种,一种叫做前人栽树后人乘凉,一种叫做前人挖坑后人遭殃.不多说了,多说又要变 ...

  4. SCRIPT7002: XMLHttpRequest: 网络错误 0x2efe, 由于出现错误 00002efe 而导致此项操作无法完成

    google中带中文参数可能查询,但是在IE带中文参数不能查询:报如下错误 SCRIPT7002: XMLHttpRequest: 网络错误 0x2efe, 由于出现错误 00002efe 而导致此项 ...

  5. Android Service执行unbind后再次执行bind的问题

    在执行了startService.bindService.unbindService之后,再次执行bindService.这时发现Service的onBind方法并没有执行,而是执行的onRebind ...

  6. ie6 ie7 userdata 本地存储 引发的惨案.

    我使用 documentElement 作为userdata 作为本地存储的载体. document.documentElement.addBehavior("#default#userda ...

  7. 计算机网络【3】—— IP地址分类与子网划分

    一.IP地址分类

  8. springmvc+mybatis 处理图片(二):显示图片

    数据库及配置文件等参考:springmvc+mybatis 处理图片(一):上传图片思路:把图片二进制信息写入到HttpServletResponse 的outputStream输出流中来显示图片.一 ...

  9. Window系统 安装TFLearn

    1. 确保成功安装了tensorflow 2. 查看当前tensorflow下的库文件,判断是否已经安装了h5py,scipy:conda list 3. 若没有安装,安装h5py,scipy.我的电 ...

  10. jsp中的下载链接

    1.下载链接jsp界面(a链接直接链文件可以看出文件在服务器中的路径,用servlet处理的链接则看不出) <%@ page language="java" contentT ...