Trie树结构
PrefixTree
208. 实现 Trie (前缀树)
Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。
请你实现 Trie 类:
Trie()初始化前缀树对象。void insert(String word)向前缀树中插入字符串word。boolean search(String word)如果字符串word在前缀树中,返回true(即,在检索之前已经插入);否则,返回false。boolean startsWith(String prefix)如果之前已经插入的字符串word的前缀之一为prefix,返回true;否则,返回false。
示例:
输入
["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
输出
[null, null, true, false, true, null, true]
解释
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple"); // 返回 True
trie.search("app"); // 返回 False
trie.startsWith("app"); // 返回 True
trie.insert("app");
trie.search("app"); // 返回 True
提示:
1 <= word.length, prefix.length <= 2000word和prefix仅由小写英文字母组成insert、search和startsWith调用次数 总计 不超过3 * 104次
实现代码:支持可变 wordList 的 Trie
class Trie {
private final Node root = new Node('/');
public void insert(char[] text) {
Node p = root;
for (char c : text) {
int index = c - 'a';
if (p.children[index] == null) {
Node newNode = new Node(c);
p.children[index] = newNode;
}
p = p.children[index];
}
p.isEndingChar = true;
}
public boolean find (char[] pattern) {
Node p = root;
for (char c : pattern) {
int index = c - 'a';
if (p.children[index] == null) {
return false;
}
}
return p.isEndingChar;
}
private static class Node {
public char data;
public boolean isEndingChar = false;
public Node[] children = new Node[26];
public Node(char r) {
this.data = r;
}
}
}
面试题 17.17. 多次搜索
给定一个较长字符串big和一个包含较短字符串的数组smalls,设计一个方法,根据smalls中的每一个较短字符串,对big进行搜索。输出smalls中的字符串在big里出现的所有位置positions,其中positions[i]为smalls[i]出现的所有位置。
示例:
输入:
big = "mississippi"
smalls = ["is","ppi","hi","sis","i","ssippi"]
输出: [[1,4],[8],[],[3],[1,4,7,10],[5]]
提示:
0 <= len(big) <= 10000 <= len(smalls[i]) <= 1000smalls的总字符数不会超过 100000。- 你可以认为
smalls中没有重复字符串。 - 所有出现的字符均为英文小写字母。
题解思想:Trie 树
class Solution {
class TrieNode {
String end;
TrieNode[] next = new TrieNode[26];
}
class Trie {
TrieNode root;
public Trie(String[] words) {
root = new TrieNode();
for (String word : words) {
TrieNode node = root;
for (char r : word.toCharArray()) {
int i = r - 'a';
if (node.next[i] == null) {
node.next[i] = new TrieNode();
}
node = node.next[i];
}
node.end = word;
}
}
public List<String> search(String str) {
TrieNode node = root;
List<String> res = new ArrayList<>();
for (char c : str.toCharArray()) {
int i = c - 'a';
if (node.next[i] == null) {
break;
}
node = node.next[i];
if (node.end != null) {
res.add(node.end);
}
}
return res;
}
}
public int[][] multiSearch(String big, String[] smalls) {
Trie trie = new Trie(smalls);
Map<String, List<Integer>> hit = new HashMap<>();
for (int i = 0; i < big.length(); i ++) {
List<String> matchs = trie.search(big.substring(i));
for (String word : matchs) {
if (!hit.containsKey(word)) {
hit.put(word, new ArrayList<>());
}
hit.get(word).add(i);
}
}
int[][] res = new int[smalls.length][];
for (int i = 0; i < smalls.length; i ++) {
List<Integer> list = hit.get(smalls[i]);
if (list == null) {
res[i] = new int[0];
continue;
}
int size = list.size();
res[i] = new int[size];
for (int j = 0; j < size; j ++) {
res[i][j] = list.get(j);
}
}
return res;
}
}
Trie树结构的更多相关文章
- Trie和Ternary Search Tree介绍
Trie树 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie树与二叉搜索树不同,键不是直接保存在节 ...
- Trie树(转:http://blog.csdn.net/arhaiyun/article/details/11913501)
Trie 树, 又称字典树,单词查找树.它来源于retrieval(检索)中取中间四个字符构成(读音同try).用于存储大量的字符串以便支持快速模式匹配.主要应用在信息检索领域. Trie 有三种结构 ...
- Double-Array Trie 原理解析
http://ansjsun.iteye.com/blog/702255 Trie树是搜索树的一种,它在本质上是一个确定的有限状态自动机,每个结点代表一个状态,根据输入变量的不同,进行状态转 ...
- Trie树子节点快速获取法
今天做了一道leetcode上关于字典树的题:https://leetcode.com/problems/word-search-ii/#/description 一开始坚持不看别人的思路,完全自己写 ...
- poj_3630 trie树
题目大意 给定一系列电话号码,查看他们之间是否有i,j满足,号码i是号码j的前缀子串. 题目分析 典型的trie树结构.直接使用trie树即可.但是需要注意,若使用指针形式的trie树,则在大数据量下 ...
- python中文分词:结巴分词
中文分词是中文文本处理的一个基础性工作,结巴分词利用进行中文分词.其基本实现原理有三点: 基于Trie树结构实现高效的词图扫描,生成句子中汉字所有可能成词情况所构成的有向无环图(DAG) 采用了动态规 ...
- Python 结巴分词
今天的任务是对txt文本进行分词,有幸了解到"结巴"中文分词,其愿景是做最好的Python中文分词组件.有兴趣的朋友请点这里. jieba支持三种分词模式: *精确模式,试图将句子 ...
- 转:鏖战双十一-阿里直播平台面临的技术挑战(webSocket, 敏感词过滤等很不错)
转自:http://www.infoq.com/cn/articles/alibaba-broadcast-platform-technology-challenges 鏖战双十一-阿里直播平台面临的 ...
- Python 结巴分词模块
原文链接:http://www.gowhich.com/blog/147?utm_source=tuicool&utm_medium=referral PS:结巴分词支持Python3 源码下 ...
- 阿里巴巴笔试整理系列 Session2 高级篇
阿里一面:1. 入场就是红黑树,B数2. apache和nginx源码看过多少,平时看过什么技术论坛,还有没有看过更多的开源代码3. pthread 到自旋锁4. hadoop源码看过没5. 为什么选 ...
随机推荐
- Luogu P3368 【模板】树状数组 2 [区间修改-单点查询]
P3368 [模板]树状数组 2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数数加上x 2.求出某一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表 ...
- css 伪类实现渐变线条
如下图所示: 需要实现渐变的小竖线或者小横线 可以用伪类, 代码如下: div { position: relative; z-index: 2; &::after{ content: ''; ...
- 作业三:CART回归树算法
作业三:CART回归树算法 班级:20大数据(3)班 学号:201613341 题目一 表1为拖欠贷款人员训练样本数据集,使用CART算法基于该表数据构造决策树模型,并使用表2中测试样本集确定剪枝后的 ...
- Java基础Day7-值传递和引用传递
一.值传递 Java都是值传递. 值传递:是指在调用函数时,将实际参数复制一份传递到函数中,这样在函数中如果对参数进行修改,就不会影响到实际参数. 值传递是对基本数据类型而言. 二.引用传递 引用传递 ...
- react 前端导出Excel
1.首先下载 js-export-excel npm install js-export-excel; 2.下载 xlsx npm install xlsx; 3.引入 import * as ...
- Git简介及使用
1.Git简介 GIT= 版本控制 + 备份服务器 我们称用来存放上传档案的地方就做Repository.用中文来说,有点像是档案仓库的意思. 不过,通常我们还是使用Repository这个名词.当有 ...
- 机制设计原理与应用(三)Screening
目录 3 Screening 3.1 为单个不可分割的项目定价 3.1.1 对\(\theta\)的假设 3.1.2 问题描述 3.1.3 特性 3.2 为无限可分的项目定价 3.2.1 对\(\th ...
- 二,使用axios
1,下载https://unpkg.com/axios@1.3.2/dist/axios.min.js保存在js目录下,命名为axios.js 2,http.js let baseUrl = &quo ...
- Verilog中端口的连接规则
摘自于(15条消息) Verilog中端口应该设置为wire形还是reg形_CLL_caicai的博客-CSDN博客, 以及(15条消息) Verilog端口连接规则_「已注销」的博客-CSDN博客_ ...
- 解决黑苹果macOS Monterey系统无法正常睡眠、睡眠无法唤醒,唤醒后显示器无输出问题
1.解决无法睡眠问题:添加睡眠补丁:HibernationFixup.kext, 或者添加ssdt:ssdt-GPRW.aml,并在ACPI补丁中添加热补丁: 2.解决睡眠后无法唤醒.唤醒后显示器无输 ...