208. 实现 Trie (前缀树)

实现Trie树,网上教程一大堆,没啥可说的

public class Trie {

    private class Node {
private int dumpli_num;////该字串的重复数目, 该属性统计重复次数的时候有用,取值为0、1、2、3、4、5……
private int prefix_num;///以该字串为前缀的字串数, 应该包括该字串本身!!!!!
private Node childs[];////此处用数组实现,当然也可以map或list实现以节省空间
private boolean isLeaf;///是否为单词节点 public Node() {
dumpli_num = 0;
prefix_num = 0;
isLeaf = false;
childs = new Node[26];
}
} private Node root;///树根 public Trie() {
///初始化trie 树
root = new Node();
} /**
* 插入字串,用循环代替迭代实现
*
* @param words
*/
public void insert(String words) {
insert(this.root, words);
} /**
* 插入字串,用循环代替迭代实现
*
* @param root
* @param words
*/
private void insert(Node root, String words) {
words = words.toLowerCase();////转化为小写
char[] chrs = words.toCharArray(); for (int i = 0, length = chrs.length; i < length; i++) {
///用相对于a字母的值作为下标索引,也隐式地记录了该字母的值
int index = chrs[i] - 'a';
if (root.childs[index] != null) {
////已经存在了,该子节点prefix_num++
root.childs[index].prefix_num++;
} else {
///如果不存在
root.childs[index] = new Node();
root.childs[index].prefix_num++;
} ///如果到了字串结尾,则做标记
if (i == length - 1) {
root.childs[index].isLeaf = true;
root.childs[index].dumpli_num++;
}
///root指向子节点,继续处理
root = root.childs[index];
} } public HashMap<String, Integer> getAllWords() {
return preTraversal(this.root, "");
} /**
* 前序遍历。。。
*
* @param root 子树根节点
* @param prefixs 查询到该节点前所遍历过的前缀
* @return
*/
private HashMap<String, Integer> preTraversal(Node root, String prefixs) {
HashMap<String, Integer> map = new HashMap<String, Integer>(); if (root != null) { if (root.isLeaf == true) {
////当前即为一个单词
map.put(prefixs, root.dumpli_num);
} for (int i = 0, length = root.childs.length; i < length; i++) {
if (root.childs[i] != null) {
char ch = (char) (i + 'a');
////递归调用前序遍历
String tempStr = prefixs + ch;
map.putAll(preTraversal(root.childs[i], tempStr));
}
}
} return map;
} /**
* 查询某字串是否在字典树中
*
* @param word
* @return true if exists ,otherwise false
*/
public boolean search(String word) {
char[] chs = word.toLowerCase().toCharArray();
Node tmpRoot = root;
for (int i = 0, length = chs.length; i < length; i++) {
int index = chs[i] - 'a';
if (tmpRoot.childs[index] == null) {
///如果不存在,则查找失败
return false;
}
tmpRoot = tmpRoot.childs[index];
} // 不能有孩子了
return tmpRoot.isLeaf;
} public boolean startsWith(String prefix) {
char[] chrs = prefix.toLowerCase().toCharArray();
Node tmpRoot = root;
for (int i = 0, length = chrs.length; i < length; i++) {
int index = chrs[i] - 'a';
if (tmpRoot.childs[index] == null) {
return false;
}
tmpRoot = tmpRoot.childs[index];
}
return true;
} /**
* 得到以某字串为前缀的字串集,包括字串本身! 类似单词输入法的联想功能
*
* @param prefix 字串前缀
* @return 字串集以及出现次数,如果不存在则返回null
*/
public HashMap<String, Integer> getWordsForPrefix(String prefix) {
return getWordsForPrefix(this.root, prefix);
} /**
* 得到以某字串为前缀的字串集,包括字串本身!
*
* @param root
* @param prefix
* @return 字串集以及出现次数
*/
private HashMap<String, Integer> getWordsForPrefix(Node root, String prefix) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
char[] chrs = prefix.toLowerCase().toCharArray();
////
for (int i = 0, length = chrs.length; i < length; i++) { int index = chrs[i] - 'a';
if (root.childs[index] == null) {
return null;
} root = root.childs[index]; }
///结果包括该前缀本身
///此处利用之前的前序搜索方法进行搜索
return preTraversal(root, prefix);
}
}

[leetcode] 208. 实现 Trie (前缀树)(Java)的更多相关文章

  1. Java实现 LeetCode 208 实现 Trie (前缀树)

    208. 实现 Trie (前缀树) 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie() ...

  2. leetcode 208. 实现 Trie (前缀树)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...

  3. 力扣 - 208. 实现Trie(前缀树)

    目录 题目 思路 代码 复杂度分析 题目 208. 实现 Trie (前缀树) 思路 在我们生活中很多地方都用到了前缀树:自动补全,模糊匹配,九宫格打字预测等等... 虽然说用哈希表也可以实现:是否出 ...

  4. 力扣208——实现 Trie (前缀树)

    这道题主要是构造前缀树节点的数据结构,帮助解答问题. 原题 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = ...

  5. 4.14——208. 实现 Trie (前缀树)

    前缀树(字典树)是经典的数据结构,以下图所示: 本来处理每个节点的子节点集合需要用到set,但是因为输入规定了只有26个小写字母,可以直接用一个[26]的数组来存储. 关于ASCII代码: Java ...

  6. 208. 实现 Trie (前缀树)

    主要是记录一下这个数据结构. 比如这个trie树,包含三个单词:sea,sells,she. 代码: class Trie { bool isWord; vector<Trie*> chi ...

  7. 力扣208. 实现 Trie (前缀树)

    原题 以下是我的代码,就是简单的字符串操作,可以ac但背离了题意,我之前没接触过Trie 1 class Trie: 2 3 def __init__(self): 4 ""&qu ...

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

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

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

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

随机推荐

  1. 利用Apache部署静态网站(一)

    Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一.它快速.可靠并且可通过简单的API扩充, ...

  2. H5小技巧之——巧用<a>标签锚链接(#锚点链接 #页面特定位置 #DOM定位 #hash路由中使用锚链接)

    #作者:矩阵鱼--代码中游泳的咸鱼 前端开发中,常遇到定位到页面某特定位置的需求,JavaScript提供的el.scrollIntoView() 和 el.scrollIntoViewIfNeede ...

  3. web前端的超神之路

    前端超神之路 前端基础知识 HTML :用户实现页面的工具 CSS:用于美化界面的工具 javascript:用于操作html元素和css样式,让你的页面效果更美观 前端进阶知识 jQuery:用于简 ...

  4. IPC 方法分类

    IPC 方法分类 进程间通信 shell out 被调用程序在执行完毕之前接管用户的键盘和显示,退出后,调用程序重新控制键盘和显示并继续运行. 专门程序通常有文件系统与父进程进行通信,方法是在指定位置 ...

  5. 我的自定义多交互live2d折腾经历

    在@m0d1 大佬的督促(?)下有了这篇复盘.不过因为可能很多地方讲得不全面+理解不够深入,故不打算把这篇当成是教程/指南,那就算是一个指北吧= = (划重点:不是教程!不是教程!不是教程! 省流简介 ...

  6. python3函数可变输入参量

    技术背景 通常我们在python中定义一个函数的时候,需要给出明确的函数输入参量,比如对于一个数学函数\(z=f(x,y)\)就表示,\(z\)是关于\(x\)和\(y\)的一个函数.但是如果对于未知 ...

  7. hdu4503 概率

    题意: 湫湫系列故事--植树节                                         Time Limit: 1000/500 MS (Java/Others) Memory ...

  8. LA3403天平难题(4个DFS)

    题意:      给出房间的宽度r和每个吊坠的重量wi,设计一个尽量宽但宽度不能超过房间宽度的天平,挂着所有挂坠,每个天平的一段要么挂这一个吊坠,要么挂着另一个天平,每个天平的总长度是1,细节我给出题 ...

  9. Linux中正则表达式和字符串的查询、替换(tr/diff/wc/find)

    目录 正则表达式 基本正则表达式 扩展正则表达式 grep tr diff du wc find 正则表达式 正则表达式,又称正规表示法.常规表示法( Regular Expression,在代码中常 ...

  10. POJ2118基础矩阵快速幂

    题意:        an=Σ1<=i<=kan-ibi mod 10 000 for n >= k,题意看了好久才懂,有点蛋疼啊, 这个题目要是能看懂题意就简单了,先给你k,然后给 ...