[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 ...
随机推荐
- ESXI5.5设置主机的时间自动同步服务 NTP
背景:现在公司的很多线上服务也都通过虚拟化来实现,最近遇到一个小问题,虚拟机上的时间不准确.原来是虚拟机会主动同步宿主机时间,一般虚拟机中都安装vmware tool工具,这个工具会自动和宿主机进行时 ...
- [UE4]把工程升级到最新版本
右键UE4工程文件,选择“Switch Unreal Engine version...” 确定后,再次双击打开工程升级到最新版本了.
- sql中存储过程打印返回的记录集
declare --返回结果,记录类型 ret sys_refcursor; --定义一种类型,用来存放返回的记录 type typ_row ), QUEUEID ), QUEUE_NAME )); ...
- 基本数据结构:链表(list)
copy from:http://www.cppblog.com/cxiaojia/archive/2012/07/31/185760.html 基本数据结构:链表(list) 谈到链表之前,先说一下 ...
- 基于Linux的Samba开源共享解决方案测试(三)
在极限写场景下,对于网关的网络监控如图: 在极限写场景下,对于网关的网络监控如图: 在极限混合读写场景下,对于网关的网络监控如图: 在极限混合读写场景下,对于客户端的网络监控如图: 双NAS网关100 ...
- position属性详解
内容: 1.position属性介绍 2.position属性分类 3.relative相对定位 4.absolute绝对定位 5.relative和absolute联合使用进行定位 6.fixed固 ...
- html:模板
http://www.mycodes.net/code_previewmap.php?id=3461 http://www.17sucai.com/pins/4120.html 欧美风格的CMS企业 ...
- 数据分析利器之hive优化十大原则
hive之于数据民工,就如同锄头之于农民伯伯.hive用的好,才能从地里(数据库)里挖出更多的数据来. 用过hive的朋友,我想或多或少都有类似的经历:一天下来,没跑几次hive,就到下班时间了. h ...
- angular controller 之间的通信方式
AngularJS中的controller是个函数,用来向视图的作用域($scope)添加额外的功能,我们用它来给作用域对象设置初始状态,并添加自定义行为. 当我们在创建新的控制器时,angularJ ...
- Delphi 浏览器WebBrowser
WebBrowser1.Navigate(URL); while WebBrowser1.busy do Application.ProcessMessages; while WebBrowser1. ...