单词的添加与查找 · 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 ...
随机推荐
- HDU 3986
http://acm.hdu.edu.cn/showproblem.php?pid=3986 从开始的最短路里依次删一条边,求新的最短路,求最长的最短路 删边操作要标记节点以及节点对应的边 #incl ...
- supervisor 使用tips
Supervisor (http://supervisord.org) 是一个用 Python 写的进程管理工具,可以很方便的用来启动.重启.关闭进程,supervisor可以同时监控多个进程,并可以 ...
- java面试题6
1.写一个冒泡排序的算法 升序排列: int[] nums = {5,6,9,10,20,30,28,27,15}; for(int i = 0;i<nums.length;i++){ for( ...
- vue+webpack多个项目共用组件动态打包单个项目
原文复制:https://www.jianshu.com/p/fa19a07b1496 修改了一些东西,因为sh脚本不能再window电脑执行,所以改成了node脚本.这是基于vue-cli2.0配置 ...
- ZedGraph右键菜单怎样禁止它弹出(转)
private void ZGC_ContextMenuBuilder( ZedGraphControl sender, ContextMenuStrip me ...
- linux 使用中括号进行条件判断
格式 “#”代表空格,不可缺少 [# param1#op# param2 #] 这种带比较操作符的形式,op左右必须使用空格隔开. 如 [# “3”==”2” #] 这种缺少空格的写法会得到结 ...
- java.lang.NumberFormatException:For input string:"undefined"
在将字符串转换为数字时导致此错误,解决此问题的思路: 1.添加 try catch语句 2.判断字符串是否为数字,将介绍java中判断字符串是否为数字的方法的几种方法 发生错误的代码: java.la ...
- 八、jdk工具之JvisualVM、JvisualVM之二--Java程序性能分析工具Java VisualVM
目录 一.jdk工具之jps(JVM Process Status Tools)命令使用 二.jdk命令之javah命令(C Header and Stub File Generator) 三.jdk ...
- 安卓控件获取器uiautomatorviewer初体验:"unable to connect to the adb. check if adb is installed correctly"
解决方法:转自:https://plus.google.com/108487870030743970488/posts/2TrMqs1ZGQv Challenge Accepted:1. Screen ...
- ROS使用国内的DDNS服务
未测试.转载余松老师的作品 虽然RouterOS 加入了cloud功能,但最近在配置RB2011的时候发现不好使,更新域名后无法正确解析到我的IP地址,虽然在cloud的public address中 ...