[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 ...
随机推荐
- OpenCV:初试牛刀-带滚动条的视频播放-2
视频播放时点击窗口关闭按钮(即小叉号)关闭窗口 隐藏console控制台 使用VideoCapture和createTrackbar实现滚动条控制视频播放 #include<iostream&g ...
- 【基础知识六】支持向量机SVM
开发库: libsvm, liblinear GitHub地址 SVM难点:核函数选择 一.基本问题 找到约束参数ω和b,支持向量到(分隔)超平面的距离最大:此时的分隔超平面称为“最优超平面 ...
- ZooKeeper系列(2) 安装部署 (转)
原文地址:http://www.cnblogs.com/wuxl360/p/5817489.html 一.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模 ...
- 查windows系统开关机记录
https://blog.csdn.net/dufufd/article/details/53316079 者是按下"开始菜单"-"程序"-"管理工具 ...
- 使用matplotlib 制图(柱状图、箱型图)
柱状图: import pandas as pd import matplotlib.pyplot as plt data = pd.read_csv('D:\\myfiles\\study\\pyt ...
- Simple2D-23(重构)反走样几何图形
以前 Simple2D 使用 Canvas2D 对象来绘制几何图形,而且渲染出来的几何图形存在明显的锯齿.如果想要抗锯齿的几何图形,则需要开启 OpenGL 的 MSAA,这需要很大的开销. 如果不使 ...
- DB分布式 跨库分页
DB分布式-两种方式 1. JDBC扩展 sharding-jdbc: 直接封装JDBC,代码迁移成本低,适用于任何连接池及ORM框架,JAR包提供服务,未使用中间层,不用额外部署,DBA无需 ...
- 第二次安装docker时,报Transaction check error的解决方法
如果在yum安装软件的时候,出现了Transaction check error:这种情况,说明rpm软件包出现了冲突,解决方法是: vi /etc/yum.repos.d/epel.repo 将en ...
- Haskell语言学习笔记(32)Data.Maybe, Data.Either
Data.Maybe Prelude Data.Maybe> maybe "" show (Just 5) "5" Prelude Data.Maybe& ...
- Python使用Threading模块创建线程
使用Threading模块创建线程,直接从threading.Thread继承,然后重写__init__方法和run方法: #!/usr/bin/python # -*- coding: UTF-8 ...