LeetCode-Add and Search Word
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.
For 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.
Solution:
public class WordDictionary {
public class TrieNode{
TrieNode[] childs;
boolean hasWord;
public TrieNode(){
childs = new TrieNode[26];
hasWord = false;
}
}
TrieNode root;
public WordDictionary(){
root = new TrieNode();
}
// Adds a word into the data structure.
public void addWord(String word) {
TrieNode cur = root;
for (char c : word.toCharArray()){
if (cur.childs[c-'a']==null){
cur.childs[c-'a'] = new TrieNode();
}
cur = cur.childs[c-'a'];
}
cur.hasWord = true;
}
// 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 searchRecur(root,word,0);
}
public boolean searchRecur(TrieNode curNode, String word, int start){
if (curNode==null){
return false;
}
if (start>=word.length()){
return curNode.hasWord;
}
char c = word.charAt(start);
if (c=='.'){
for (TrieNode child : curNode.childs)
if (searchRecur(child,word,start+1)){
return true;
}
return false;
} else {
return searchRecur(curNode.childs[c-'a'],word,start+1);
}
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
LeetCode-Add and Search Word的更多相关文章
- [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——Add and Search Word - Data structure design
Description: Design a data structure that supports the following two operations: void addWord(word) ...
- leetcode Add and Search Word - Data structure design
我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...
- LeetCode Add and Search Word - Data structure design (trie树)
题意:实现添加单词和查找单词的作用,即实现字典功能. 思路:'.' 可以代表一个任何小写字母,可能是".abc"或者"a.bc"或者"abc.&quo ...
- leetcode面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...
- 字典树(查找树) 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】211. Add and Search Word - Data structure design
Add and Search Word - Data structure design Design a data structure that supports the following two ...
- [LintCode] Add and Search Word 添加和查找单词
Design a data structure that supports the following two operations: addWord(word) and search(word) s ...
- (Data structure)Implement Trie && Add and Search Word
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
随机推荐
- Guid.NewGuid().ToString()得几种格式显示
1.Guid.NewGuid().ToString("N") 结果为: 38bddf48f43c48588e0d78761eaa1ce6 2.Guid.NewGuid ...
- django admin 如何去掉s 如何去掉django admin 各个模块后面的s
其中加上红色标记的内容,业务管理员后面就不会有 s 了 class UsrMngUser(models.Model): user_name = models.CharField("用户名称& ...
- blender, 同时选择多个顶点
法1:按MRB(鼠标右键)选中第一个顶点,再按shift+MRB依次选择其余顶点. 法2:按B,光标变为横纵两条虚线,此时可按MLB(鼠标左键)框选顶点.按MRB结束框选. 法3:按C,光标变为虚线圆 ...
- 为什么在c语言中使用gets函数是危险的
If you have code like this: char s[10]; gets( s ); and you type in more than 10 characters when th ...
- Netty的ByteBuf
https://blog.csdn.net/thinking_fioa/article/details/80795673 netty的ByteBuf知识点
- poj 2524 Ubiquitous Religions 一简单并查集
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 22389 Accepted ...
- ERROR 1130 (HY000): Host '192.168.0.190' is not allowed to connect to this MySQL serv
环境: CentOS6.2.MySQL5.1 问题描述: 在配置文件中将需要连接的MySQL的host设置为192.168.0.190(其实就是我自己的IP地址),然后运行自己的程序,结果返回MySQ ...
- SAP ECC6安装系列二:安装前的准备工作
原作者博客 http://www.cnblogs.com/Michael_z/ ======================================== 安装 Java 1,安装 Java, ...
- 设置root密码,su与sudo的区别
sudo passwd root 可以修改root密码,但首先会要求你输入当前用户的密码 sudo的意思是switch user do,默认切换到root,要求当前用户的密码,会自动调用exit返回到 ...
- STUN协议简析
http://blog.csdn.net/mazidao2008/article/details/4934257 ——————————————————————————————————————————— ...