[抄题]:

Implement a trie with insertsearch, and startsWith methods.

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

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

在node数组中选取一个node点来插入

[一刷]:

  1. 搜索是在node上进行的,结果也应该保存在一个node中,用来处理返回值
  2. insert的特判是index满了,此时无法插入;不是空,空时正常插入。find的特判是满了,此时就是返回自身,不是空,因为children[pos]为空就是普通情况。
  3. 寻找的index == word.length()时,直接返回单词本身。

[二刷]:

  1. children[pos]装的就是TrieNode,没有时 需要变成一个同类型的节点,稍微理解下

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

感觉难是因为java基础不扎实,多做几次吧。

  1. insert和find都是在中间的拐点处进行的 而非从起点开始,有一般性。没见过
  2. TrieNode中对children[pos]操作,Trie中对root进行操作

[复杂度]:Time complexity: O(n) Space complexity: O(<n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

  1. trie的时间复杂度和hashmap一样。优点在于空间复杂度,开头相同的字符串,不必重复存。
  2. 声明成员变量、引用在方法外部,自定义的类中要有方法实例化自己 新建对象,在方法内部,后期才能用this调用。这是java的基础知识,之前没有概念。

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

211. Add and Search Word - Data structure design 就是写类,用数据结构辅助完成

[代码风格] :

class TrieNode {
//data structure
private TrieNode[] children;
public boolean hasWord;
//initialization
public TrieNode() {
children = new TrieNode[26];
hasWord = false;
}
//insert
public void insert(String word, int index) {
if (index == word.length()) {
this.hasWord = true;
return ;
}
int pos = word.charAt(index) - 'a';
if (children[pos] == null) {
children[pos] = new TrieNode();
}
children[pos].insert(word, index + 1);
}
//find
public TrieNode find(String word, int index) {
if (index == word.length()) {
return this;//this
}
int pos = word.charAt(index) - 'a';
if (children[pos] == null) {
return null;
}
return children[pos].find(word, index + 1);//to who
}
}
public class Trie {
private TrieNode root; public Trie() {
root = new TrieNode();
} /*
* @param word: a word
* @return: nothing
*/
public void insert(String word) {
root.insert(word, 0);
} /*
* @param word: A string
* @return: if the word is in the trie.
*/
public boolean search(String word) {
TrieNode node = root.find(word, 0);//to who
return ((node != null) && (node.hasWord));
} /*
* @param prefix: A string
* @return: if there is any word in the trie that starts with the given prefix.
*/
public boolean startsWith(String prefix) {
TrieNode node = root.find(prefix, 0);//to who
return (node != null);
}
}

Implement Trie (Prefix Tree)实现字典树的更多相关文章

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

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

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

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

  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面试准备:Implement Trie (Prefix Tree)

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

  6. 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  ...

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

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

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

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

  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. Linux:自动获取静态IP地址,清空iptable,修改selinux脚本

    自动获取静态IP地址,清空iptable,修改selinux脚本 环境:VMware 平台:centos6.8全新 功能: 1)应用ifconfig -a,route -n,cat /etc/reso ...

  2. 使用C++实现二叉搜索树的数据结构

    需要注意的地方: ①二叉搜索树删除一个指定结点R,若R为叶子结点,则将R的父结点中指向R的指针改为指向nullptr:若R的左右子结点一个为空,一个非空,则将R的父结点中指向R的指针改为指向R的非空子 ...

  3. Python 简单网页爬虫学习

    #coding=utf-8 # 参考文章: # 1. python实现简单爬虫功能 # http://www.cnblogs.com/fnng/p/3576154.html # 2. Python 2 ...

  4. koa/koa2项目搭建

    一键生成koa/koa2项目: 1. npm install -g koa-generator 2.新建项目目录 koa mytest (koa1项目) koa2 koa2test (koa2项目) ...

  5. TCP和UDP Client 代码

    最近学习要求做网络编程,使用从网上找了一些资料,主要是网络协议的分层等通讯,你可以查看英文版的资料:CScharp网络编程英文版 下面直接给出代码吧,我想一看应该就懂. TCP Client 代码: ...

  6. dig命令使用大全

    https://www.cnblogs.com/daxian2012/archive/2013/01/10/2854126.html 译者序:可以这样说,翻译本篇文档的过程就是我重新学习DNS的过程, ...

  7. Windows自带NAT端口映射,命令行CMD操作即可

    由于有需求进行端口映射,又不想装乱七八糟的软件,Windows本身自带的路由远程访问配置太麻烦,还要两块网卡,坑爹啊. 其实Windows本身命令行支持配置端口映射,条件是已经安装了IPV6,启不启用 ...

  8. malloc/free与new/delete的不同及注意点

    #include<iostream> using namespace std; class Obj{ public : Obj(){cout<<"Initializa ...

  9. CentOS7.2部署FTP

    目前Linux大部分部署的FTP服务器都是vsftpd,至于为什么,暂时没什么必要深究. 1.安装vsftpd # yum check-update //检查可更新的程序,也可以不更新直接安装,以防万 ...

  10. Bootstrap-CL:导航元素

    ylbtech-Bootstrap-CL:导航元素 1.返回顶部 1. Bootstrap 导航元素 本章我们将讲解 Bootstrap 提供的用于定义导航元素的一些选项.它们使用相同的标记和基类 . ...