单词的添加与查找 · Add and Search Word
[抄题]:
设计一个包含下面两个操作的数据结构:addWord(word), search(word)
addWord(word)会在数据结构中添加一个单词。而search(word)则支持普通的单词查询或是只包含.和a-z的简易正则表达式的查询。
一个 . 可以代表一个任何的字母。
addWord("bad")
addWord("dad")
addWord("mad")
search("pad") // return false
search("bad") // return true
search(".ad") // return true
search("b..") // return true
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
api的题要写自定义函数,比较有灵活性。忘了
不知道正则表达式怎么用:for循环
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- now.children[c - 'a'] = new TrieNode(); 表示26叉树中建立了新字母。now = now.children[c - 'a'];表示赋值给当前节点。
- 所有函数中都是对指针now操作,都要返回节点.hasWord。新类中的每个变量都要处理,以前不知道。
- 如果now.children[c - 'a']已经初始化出节点,就持续find(word, index + 1, now.chidren[c - 'a']);直到退出。类似dfs, 没理解
- find的查找具有一般性,所以从now开始找,search调用时起点为0即可。自定义的函数一般都具有一般性。
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
正则表达式就是用for循环,先不要加括号。括号中有一个成立就是true, 括号外全都不成立才为false.
[复杂度]:Time complexity: O(n) Space complexity: O(<n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
trie字典树,正则表达式0-26有一个就行 写起来简单,hash写起来麻烦
[关键模板化代码]:
api题:
find(String word, int index, TrieNode now) {
if (index == word.length()) {
return now.hasWord;//buyiding zhaodao
}
自定义find函数再调用
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
745. Prefix and Suffix Search 前缀 用sum
[代码风格] :
//class TrieNode
class TrieNode {
TrieNode[] children = new TrieNode[26];
boolean hasWord; TrieNode () {
for (int i = 0; i < 26; i++) {
children[i] = null;
}
hasWord = false;
}
} public class WordDictionary {
/*
* @param word: Adds a word into the data structure.
* @return: nothing
*/
TrieNode root; WordDictionary () {
root = new TrieNode();
} public void addWord(String word) {
TrieNode now = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
//no TrieNode
if (now.children[c - 'a'] == null) {
now.children[c - 'a'] = new TrieNode();
}
now = now.children[c - 'a'];
}
now.hasWord = true;
}
//find(word, index, i)
private boolean find(String word, int index, TrieNode now) {
if (index == word.length()) {
return now.hasWord;//buyiding zhaodao
}
char c = word.charAt(index);
if (c == '.') {
for (int i = 0; i < 26; i++)
if (now.children[i] != null) {
if (find(word, index + 1, now.children[i])) {
return true; }
}
return false;
}else if (now.children[c - 'a'] != null) {
return find(word, index + 1, now.children[c - 'a']);
}else {
return false;
}
} public boolean search(String word) {
return find(word, 0, root);
}
}
单词的添加与查找 · Add and Search Word的更多相关文章
- [LintCode] Add and Search Word 添加和查找单词
Design a data structure that supports the following two operations: addWord(word) and search(word) s ...
- 字典树(查找树) 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 ...
- leetcode面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...
- (Data structure)Implement Trie && Add and Search Word
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
- LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design
字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith ...
- 【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 ...
- [LeetCode] 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添加查找单词 - 数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
随机推荐
- Relation.js——基于pixi.js的拓展模块之人物关系图谱
出于[重构基于D3的关系图谱项目]的目的,在看完pixi.js之后,并且网上又没有现成的基于webgl的关系图谱js库,于是,本人决定自己写一个. 因为平常要工作的原因,进度可能有点慢,但是githu ...
- threejs通过射线Ray获取指定的点
例:获取cube方向上的面的中点坐标(该cube默认方向为(0,1,0)) (中心点向cube quaternion 方向上发射射线,与正前方的面相交的点即为目标点; 由于ray只支持box和face ...
- 如何在idea中使用Mybatis-generator插件快速生成代码
传送门 使用这个插件可以快速生成一些代码,包含 实体类/Mapper接口/*Mapper.xml文件 首先,我们需要搭建一个Maven的项目. 在pom.xml中添加代码 <plugins> ...
- .NET 中让 Task 支持带超时的异步等待
Task 自带有很多等待任务完成的方法,有的是实例方法,有的是静态方法.有的阻塞,有的不阻塞.不过带超时的方法只有一个,但它是阻塞的. 本文将介绍一个非阻塞的带超时的等待方法. Task 已有的等 ...
- UT源码+105032014070
设计三角形问题的程序 输入三个整数a.b.c,分别作为三角形的三条边,现通过程序判断由三条边构成的三角形的类型为等边三角形.等腰三角形.一般三角形(特殊的还有直角三角形),以及不构成三角形.(等腰直角 ...
- [CF995F]Cowmpany Cowmpensation
codeforces description 一棵\(n\)个节点的树,给每个节点标一个\([1,m]\)之间的编号,要求儿子的权值不大于父亲权值.求方案数.\(n\le3000,n\le10^9\) ...
- hdu1066
求N!的非零末尾位(吉大ACM模板) #include <stdio.h> #include <string.h> #define MAXN 10000 int lastdig ...
- pthread调度策略,优先级和竞争范围
实时调度:操作系统在有限的时间内提供特定水平的服务能力.受限制的响应时间不一定是块的反应,意味着可预知的响应速度.如果系统定义_POSIX_THRAED_PRIORITY_SCHEDULING,它为线 ...
- 使用C++生成1-33中的6个随机数,无重复
生成1-33中的6个随机数,无重复 ------------------------------------------------------------------------ 方法1.每生成 ...
- 流行的FPGA的上电复位
在实际设计中,由于外部阻容复位时间短,可能无法使FPGA内部复位到理想的状态,所以今天介绍一下网上流行的复位逻辑. 在基于verilog的FPGA设计中,我们常常可以看到以下形式的进程: 信号rst_ ...