[抄题]:

设计一个包含下面两个操作的数据结构: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循环

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. now.children[c - 'a'] = new TrieNode(); 表示26叉树中建立了新字母。now = now.children[c - 'a'];表示赋值给当前节点。
  2. 所有函数中都是对指针now操作,都要返回节点.hasWord。新类中的每个变量都要处理,以前不知道。
  3. 如果now.children[c - 'a']已经初始化出节点,就持续find(word, index + 1, now.chidren[c - 'a']);直到退出。类似dfs, 没理解
  4. 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的更多相关文章

  1. [LintCode] Add and Search Word 添加和查找单词

    Design a data structure that supports the following two operations: addWord(word) and search(word) s ...

  2. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  3. 【刷题-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 ...

  4. leetcode面试准备:Add and Search Word - Data structure design

    leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...

  5. (Data structure)Implement Trie && Add and Search Word

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...

  6. 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  ...

  7. 【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 ...

  8. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  9. [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 ...

随机推荐

  1. Winform创建解决方案

    Winform的开发工具可以使用VS2005---VS2013,版本在不断升级,VS的功能也越来越强大.本系列文章采用VS2012(以后全称VS)演示. 窗体是winform开发的基础,需要掌握窗体的 ...

  2. scikit-learn 学习笔记-- Generalized Linear Models (二)

    Lasso regression 今天介绍另外一种带正则项的线性回归, ridge regression 的正则项是二范数,还有另外一种是一范数的,也就是lasso 回归,lasso 回归的正则项是系 ...

  3. Coding(码市)教程(一):基础配置

    作者:Adaaaagio 出处:http://www.cnblogs.com/zyx418 欢迎转载,希望能够帮到更多的人,转载也请保留这段申明,谢谢! 初识coding是在新入职的公司,前辈说我们用 ...

  4. python的pip源在windows和linux修改

    windows和linux修改python的pip源 https://www.cnblogs.com/cwp-bg/p/8497075.html windows和linux修改python的pip源 ...

  5. 《DSP using MATLAB》示例 Example 9.14

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  6. 使用树莓派制作一套“NAS+私有云盘+下载机”

    ‍ ‍‍原创作者:HackLiu‍‍ ‍ 0×00 前言 ‍‍如果你家里有多台设备需要联网需要娱乐,你一定会或多或少遇到设备碎片化带来的烦恼.当然,已经有很多厂商包括新晋的小米.360在内的互联网公司 ...

  7. mysql having,group by查询去除重复记录

    http://m.jb51.net/article/39302.htm 可以这样去理解group by和聚合函数 http://www.cnblogs.com/wuguanglei/p/4229938 ...

  8. 【转】VC++ 也有 Refactoring 功能了

    原文网址:http://blog.csdn.net/yapingxin/article/details/18923095 分类: C++2014-02-04 14:00 2688人阅读 评论(0) 收 ...

  9. java对象模型

    java对象模型其实就是JVM中对象的内存布局.一个对象本身内在结构的描述信息以字节码的方式存储在方法区中(参见java内存区域),说白了就是class文件.那么如何获取到对象的class信息呢?虚拟 ...

  10. 编译sass,遇到报错error style.scss (Line 3: Invalid GBK character "\xE5")

    今天学习sass,写了一行中文注释,结果却遇到了报错: cmd.exe /D /C call C:/Ruby23-x64/bin/scss.bat --no-cache --update style. ...