[leetcode] 208. 实现 Trie (前缀树)(Java)
实现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)的更多相关文章
- Java实现 LeetCode 208 实现 Trie (前缀树)
208. 实现 Trie (前缀树) 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie() ...
- leetcode 208. 实现 Trie (前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...
- 力扣 - 208. 实现Trie(前缀树)
目录 题目 思路 代码 复杂度分析 题目 208. 实现 Trie (前缀树) 思路 在我们生活中很多地方都用到了前缀树:自动补全,模糊匹配,九宫格打字预测等等... 虽然说用哈希表也可以实现:是否出 ...
- 力扣208——实现 Trie (前缀树)
这道题主要是构造前缀树节点的数据结构,帮助解答问题. 原题 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = ...
- 4.14——208. 实现 Trie (前缀树)
前缀树(字典树)是经典的数据结构,以下图所示: 本来处理每个节点的子节点集合需要用到set,但是因为输入规定了只有26个小写字母,可以直接用一个[26]的数组来存储. 关于ASCII代码: Java ...
- 208. 实现 Trie (前缀树)
主要是记录一下这个数据结构. 比如这个trie树,包含三个单词:sea,sells,she. 代码: class Trie { bool isWord; vector<Trie*> chi ...
- 力扣208. 实现 Trie (前缀树)
原题 以下是我的代码,就是简单的字符串操作,可以ac但背离了题意,我之前没接触过Trie 1 class Trie: 2 3 def __init__(self): 4 ""&qu ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
随机推荐
- python进阶(17)协程
协程 协程(Coroutine),又称微线程,纤程.(协程是一种用户态的轻量级线程) 作用:在执行 A 函数的时候,可以随时中断,去执行 B 函数,然后中断B函数,继续执行 A 函数 (可以自动切 ...
- 1036 Boys vs Girls
This time you are asked to tell the difference between the lowest grade of all the male students and ...
- Blade模板引擎教程-常用语法格式
打印变量或者默认值,这个语法会自动转义变量内容中的html标记,使得html标签原样输出Welcome, {{ $name or 'California' }} 打印变量原始内容,不进行转义的用法{! ...
- POJ2195费用流+BFS建图
题意: 给你一个n*m的地图,上面有w个人,和w个房子,每个人都要进房子,每个房子只能进一个人,问所有人都进房子的路径总和最少是多少? 思路: 比较简单的最大流,直接建立两排, ...
- UVA11388GCD LCM
题意: 输入两个整数G,L,找出两个正整数a,b使得gcd(a ,b)=G,lcm(a ,b)=L,如果有多组解,输出最小的a的那组,如果没解,输出-1. 思路: 比较简单,如 ...
- Portswigger web security academy:OS command injection
Portswigger web security academy:OS command injection 目录 Portswigger web security academy:OS command ...
- 【python】Leetcode每日一题-螺旋矩阵
Leetcode每日一题-螺旋矩阵 [题目描述] 给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素. 示例1: 输入:matrix = [[1,2,3], ...
- 在AWS Glue中使用Apache Hudi
1. Glue与Hudi简介 AWS Glue AWS Glue是Amazon Web Services(AWS)云平台推出的一款无服务器(Serverless)的大数据分析服务.对于不了解该产品的读 ...
- h5基本内容
一 简介 html 超文本标记语言 W3C 中立技术标准机构 W3C标准包括 结构化标准语言(HTML,XML) 表现标准语言(CSS) 行为标准(DOM,ECMAScript) 二 入门例子 < ...
- net5学习笔记---依赖注入
小王的故事 小王去上班 小王是个程序员,每个工作日他都要去上班,诸多交通工具他最喜欢的交通工具是骑电车.在骑行的过程中放空自己使他很快. 突然有一天天气预报说近期有很大的雨,小王再想骑电车去上 ...