Trie 类型的题目总结

2024-09-04 14:48
文章标签 类型 总结 题目 trie

本文主要是介绍Trie 类型的题目总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

trie字典树可以用来查找单词或者搜索剪枝用。

Implement Trie (Prefix Tree) 实现一个 Trie,包含 insertsearch, 和 startsWith 这三个方法。(模板必须记住;没有儿子建立儿子,有儿子走儿子;)

class Trie {private class TrieNode {public TrieNode[] children;public boolean isword;public TrieNode () {this.children = new TrieNode[26];this.isword = false;}}/** Initialize your data structure here. */private TrieNode root;public Trie() {root = new TrieNode();}/** Inserts a word into the trie. */public void insert(String word) {if(word == null || word.length() == 0) {return;}TrieNode cur = root;for(int i = 0; i < word.length(); i++) {char c = word.charAt(i);if(cur.children[c - 'a'] == null) {cur.children[c - 'a'] = new TrieNode();}cur = cur.children[c - 'a'];}cur.isword = true;}/** Returns if the word is in the trie. */public boolean search(String word) {TrieNode cur = searchPrefix(word);return cur != null && cur.isword;}/** Returns if there is any word in the trie that starts with the given prefix. */public boolean startsWith(String prefix) {TrieNode cur = searchPrefix(prefix);return cur != null;}private TrieNode searchPrefix(String prefix) {if(prefix == null || prefix.length() == 0) {return null;}TrieNode cur = root;for(int i = 0; i < prefix.length(); i++) {char c = prefix.charAt(i);if(cur.children[c - 'a'] == null) {return null;}cur = cur.children[c - 'a'];}return cur;}
}/*** 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);*/

Add and Search Word - Data structure design (遇见 ‘.’ 之后,for循环check每一个可能性;注意这题我写了两个坑:

1. index == word.length()的时候,返回的是cur.isword, 而不是直接返回true;

2. for循环的时候,一定要判断cur.children[i] != null, 也就是判断存入的单词,是否这条路径;搜索所有的路径,那就是DFS搜索,每一种情况都要check,只要有一种情况是true,那么就返回true;)

思路:这道题如果做过之前的那道
Implement Trie (Prefix Tree) 实现字典树(前缀树)的话就没有太大的难度了,因为这道题里面'.'可以代替任意字符,所以一旦有了'.',就需要查找之前存下的所有下一层的不是null的path;String match 的题,一般都是DFS 参数里面加入index,然后递归 subproblem求解;

class WordDictionary {private class TrieNode {public TrieNode[] children;public boolean isword;public String word;public TrieNode() {this.children = new TrieNode[26];this.isword = false;this.word = null;}}private class Trie {public TrieNode root;public Trie() {this.root = new TrieNode();}public void insert(String word) {TrieNode cur = root;for(int i = 0; i < word.length(); i++) {char c = word.charAt(i);if(cur.children[c - 'a'] == null) {cur.children[c - 'a'] = new TrieNode();}cur = cur.children[c - 'a'];}cur.isword = true;cur.word = word;}public boolean search(String word) {return ismatch(word, 0, root);}private boolean ismatch(String word, int index, TrieNode cur) {if(index == word.length()) {return cur.isword;}char c = word.charAt(index);if(c == '.') {for(int i = 0; i < 26; i++) {if(cur.children[i] != null) { // 搜索存储的,下一层所有不是null的path;if(ismatch(word, index + 1, cur.children[i])) {return true;}}}return false;} else {if(cur.children[c - 'a'] == null) {return false;} else {return ismatch(word, index + 1, cur.children[c - 'a']);}}}}/** Initialize your data structure here. */private Trie trie;public WordDic

这篇关于Trie 类型的题目总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1136277

相关文章

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

Spring 依赖注入与循环依赖总结

《Spring依赖注入与循环依赖总结》这篇文章给大家介绍Spring依赖注入与循环依赖总结篇,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Spring 三级缓存解决循环依赖1. 创建UserService原始对象2. 将原始对象包装成工

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方

python中的显式声明类型参数使用方式

《python中的显式声明类型参数使用方式》文章探讨了Python3.10+版本中类型注解的使用,指出FastAPI官方示例强调显式声明参数类型,通过|操作符替代Union/Optional,可提升代... 目录背景python函数显式声明的类型汇总基本类型集合类型Optional and Union(py

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十

MyBatis的xml中字符串类型判空与非字符串类型判空处理方式(最新整理)

《MyBatis的xml中字符串类型判空与非字符串类型判空处理方式(最新整理)》本文给大家介绍MyBatis的xml中字符串类型判空与非字符串类型判空处理方式,本文给大家介绍的非常详细,对大家的学习或... 目录完整 Hutool 写法版本对比优化为什么status变成Long?为什么 price 没事?怎

C#之枚举类型与随机数详解

《C#之枚举类型与随机数详解》文章讲解了枚举类型的定义与使用方法,包括在main外部声明枚举,用于表示游戏状态和周几状态,枚举值默认从0开始递增,也可手动设置初始值以生成随机数... 目录枚举类型1.定义枚举类型(main外)2.使用生成随机数总结枚举类型1.定义枚举类型(main外)enum 类型名字

Python lambda函数(匿名函数)、参数类型与递归全解析

《Pythonlambda函数(匿名函数)、参数类型与递归全解析》本文详解Python中lambda匿名函数、灵活参数类型和递归函数三大进阶特性,分别介绍其定义、应用场景及注意事项,助力编写简洁高效... 目录一、lambda 匿名函数:简洁的单行函数1. lambda 的定义与基本用法2. lambda

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

MySQL 索引简介及常见的索引类型有哪些

《MySQL索引简介及常见的索引类型有哪些》MySQL索引是加速数据检索的特殊结构,用于存储列值与位置信息,常见的索引类型包括:主键索引、唯一索引、普通索引、复合索引、全文索引和空间索引等,本文介绍... 目录什么是 mysql 的索引?常见的索引类型有哪些?总结性回答详细解释1. MySQL 索引的概念2