[leetcode]211. Add and Search Word - Data structure design添加查找单词 - 数据结构设计
Design a data structure that supports the following two operations:
void addWord(word)
bool search(word)
search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.
Example:
addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true
Note:
You may assume that all words are consist of lowercase letters a-z.
题意:
设计一个数据结构,能够插入单词,能够查找字符串,并支持正则表达式中的“.”单字符通配。
思路:
这是一道很典型的字典树(Trie)的题目,唯一的变化是在字典树查找的过程中引入了通配符.,通配符匹配可以通过回溯法(枚举26个英文)实现。
字典树(Trie)是面试中常见的题型,也叫做前缀树,是一种用来快速检索的多叉树结构。对于本题而言,需要构造的是一个英文字母字典树,因此是一个26叉树。
字典树可以利用字符串的公共前缀来节约存储的空间并加快检索的速度。下图中的字典树包含了五个英文单词dad, do, dog, done, bi:

字典树有如下基本性质:
- 字典树的根节点不包含字符,代表一个空字符串;
- 从根节点到某节点,路径上经过的字符连接起来即为该节点所代表的字符串;
- 每个节点所代表的字符串各不相同,父节点代表的字符串一定是它孩子节点代表的字符串的前缀
代码:
class WordDictionary {
private TrieNode root;
/** Initialize your data structure here. */
public WordDictionary() {
root = new TrieNode();
}
/** Adds a word into the data structure. */
public void addWord(String word) { // bad
TrieNode node = root; // 初始化node
for(int i = 0; i< word.length();i++){ // 遍历bad的每个char
int j = word.charAt(i)-'a'; // j = 62-61 = 1
if(node.children[j]==null){ //
node.children[j]=new TrieNode();
}
node = node.children[j];
}
node.isWord = true;
node.word = word;
}
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
public boolean search(String word) {
return find(word,root,0);
}
// recursively checking each char
public boolean find(String word, TrieNode node, int index){
if(index == word.length()) return node.isWord;
if(word.charAt(index) =='.') {
for(TrieNode temp : node.children){
if(temp!=null && find(word,temp,index+1)) return true;
}
return false;
} else{
int j = word.charAt(index) - 'a';
TrieNode temp = node.children[j];
return temp !=null && find(word, temp, index+1);
}
}
}
class TrieNode{
TrieNode[] children;
boolean isWord;
String word;
public TrieNode(){
children = new TrieNode[26];
isWord = false;
word = "";
}
}
[leetcode]211. Add and Search Word - Data structure design添加查找单词 - 数据结构设计的更多相关文章
- [LeetCode] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- (*medium)LeetCode 211.Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- leetcode@ [211] Add and Search Word - Data structure design
https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...
- leetcode 211. Add and Search Word - Data structure design Trie树
题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...
- 211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两个操作的数据结构:void addWord(word)bool search(word)search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- 【LeetCode】211. Add and Search Word - Data structure design
Add and Search Word - Data structure design Design a data structure that supports the following two ...
随机推荐
- Spark 编程模型(下)
创建Pair RDD 什么是Pair RDD 创建Pair RDD Pair RDD的转化操作 Pair RDD的转化操作1 在xshell启动 reduceByKey的意思是把相同的key的valu ...
- solr4.x之原子更新
solr4.x发布以后,最值得人关注的一个功能,就是原子更新功能,传说的solr是否能真正的做到像数据库一样,支持单列更新呢? 在solr官方的介绍中,原子更新是filed级别的更新,不会涉及整个Do ...
- 记录——node-mysql连接池遇到的全局变量问题
记录一个折腾了快2个小时的BUG,目前还不清楚原理. 系统分别在阿里云(测试用).XL服务器上部署,此次BUG所在功能模块为生成表格并下载,表格数据由120(阿里云)上的数据库提供. 阿里云上一切正常 ...
- 搭建pyspider爬虫服务
1. 环境准备 首先yum更新 yum update -y 安装开发编译工具 yum install gcc gcc-c++ -y 安装依赖库 yum install python-pip pytho ...
- flask高阶
内容: 1.进程线程复习 2.flask多线程的问题 3.线程隔离 4.LocalStack 5.flask上下文整理 6.多app应用 1.进程线程复习 (1)进程 进程是一个具有一定独立功能的程序 ...
- Ingress.yaml
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: test-ingress namespace: default annotat ...
- windows10系统右键添加cmd命令
https://blog.csdn.net/Mr_BEelzebub/article/details/78776104 首先,在桌面新建一个文本文档. Windows Registry Editor ...
- leetcode130
struct POS { int x; int y; POS(int newx, int newy): x(newx), y(newy) {} }; class Solution { public: ...
- 转发 DDoS攻防战 (一) : 概述
岁寒 然后知松柏之后凋也 岁寒 然后知松柏之后凋也 ——论语·子罕 (此图摘自<Web脚本攻击与防御技术核心剖析>一书,作者:郝永清先生) DDoS,即 Distributed ...
- tensorflow笔记之滑动平均模型
tensorflow使用tf.train.ExponentialMovingAverage实现滑动平均模型,在使用随机梯度下降方法训练神经网络时候,使用这个模型可以增强模型的鲁棒性(robust),可 ...