单词的添加与查找 · 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 ...
随机推荐
- GPU编程自学7 —— 常量内存与事件
深度学习的兴起,使得多线程以及GPU编程逐渐成为算法工程师无法规避的问题.这里主要记录自己的GPU自学历程. 目录 <GPU编程自学1 -- 引言> <GPU编程自学2 -- CUD ...
- self-taught learning setting && semi-supervised learning
参考文献: 摘于上文献: The more general and powerful setting is the self-taught learning setting, which does n ...
- mysql下,保存时间时具体时间丢失,只保存了日期的问题
将日志信息记入数据库时增加了一个时间字段,发现存入数据库时只保留了日期,而没有时分秒信息. 我这边环境是(SRPINGMVC+Mybatis,mysql版本5.6.28以上),java层使用类型为ja ...
- Jmeter-Transaction Controller(事务控制器)
generate parent sample:生成父样本 include duration of timer and pre-post processors in generated sample:在 ...
- BZOJ4886: [Lydsy1705月赛]叠塔游戏(环套树森林&贪心)
4886: [Lydsy1705月赛]叠塔游戏 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 198 Solved: 76[Submit][Stat ...
- VS2010 C++环境下DLL和LIB文件的生成与调试 备忘
利用VS2010工具,调试DLL文件的方法现总结如下: 在一个解决方案中生成两个工程,假设MYDLL和MYDLG两个工程,前者是DLL工程,后者DLG调用前边的DLL工程.设置如下: 目录如下:图,本 ...
- Java 将指定字符串连接到此字符串的结尾 concat()
Java 手册 concat public String concat(String str) 将指定字符串连接到此字符串的结尾. 如果参数字符串的长度为 0,则返回此 String 对象.否则,创建 ...
- 在 Laravel 5 中集成七牛云存储实现云存储功能(非上传)
本扩展包基于https://github.com/qiniu/php-sdk开发,是七牛云储存 Laravel 5 Storage版,通过本扩展包可以在Laravel 5中集成七牛云存储功能. 1.安 ...
- ckplayer的Error #2033:Can not call javascript:ckstyle()解决
在我们添加多个视频的时候,就会出现这个报错:Error #2033:Can not call javascript:ckstyle(); 但是也不是所有的浏览器都不能正常运行,我这边就是IE10不能正 ...
- 告诉你C盘里的每个文件夹都是干什么用的 ! ! !
Documents and Settings是什么文件? 答案: 是系统用户设置文件夹,包括各个用户的文档.收藏夹.上网浏览信息.配置文件等. 补:这里面的东西不要随便删除,这保存着所有用户的文档 ...