(*medium)LeetCode 211.Add and Search Word - Data structure design
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
.
public class WordDictionary { private List<String>list=new ArrayList<>();
// Adds a word into the data structure.
public void addWord(String word) {
list.add(word);
} // 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) {
if(list.contains(word)){
return true;
}else{
int size=list.size();
int i=0,j=0;
for(;i<size;i++){
String s= list.get(i);
int len=s.length();
if(len!=word.length())
continue;
j=0;
for(;j<len;j++){
if(word.charAt(j)=='.')
continue;
else{
if(s.charAt(j)!=word.charAt(j)){
break;
}
}
}
if(j==len) return true;
}
return false;
}
}
} // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
运行结果:
方法2:按照提示,使用单词查字树
代码如下:
public class WordDictionary { public class TrieNode{
public TrieNode[] children=new TrieNode[26];
public String item="";
}
private TrieNode root=new TrieNode();
// Adds a word into the data structure.
public void addWord(String word) {
TrieNode node=root;
for(char c:word.toCharArray()){
if(node.children[c-'a']==null){
node.children[c-'a']=new TrieNode();
}
node=node.children[c-'a'];
}
node.item=word;
} // 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 match(word.toCharArray(),0,root);
}
private boolean match(char[] chs,int k,TrieNode node){
if(k==chs.length) return !node.item.equals("");
if(chs[k]!='.'){
return node.children[chs[k]-'a']!=null && match(chs,k+1,node.children[chs[k]-'a']);
}else{
for(int i=0;i<node.children.length;i++){
if(node.children[i]!=null){
if(match(chs,k+1,node.children[i])){
return true;
}
}
}
}
return false;
}
} // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("patter");

(*medium)LeetCode 211.Add and Search Word - Data structure design的更多相关文章
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- leetcode@ [211] Add and Search Word - Data structure design
https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...
- [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 ...
- leetcode 211. Add and Search Word - Data structure design Trie树
题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...
- [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 ...
- 字典树(查找树) 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 ...
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
随机推荐
- newman 3.0改变
https://github.com/postmanlabs/newman/blob/develop/MIGRATION.md
- disruptor - Concurrent Programming Framework 并发编程框架
disruptor发布了Java的2.0版本(.Net版本见这里),disruptor是一个高性能的异步处理框架,或者可以认为是最快的消息框架(轻量的JMS),也可以认为是一个观察者模式实现,或者事件 ...
- Windows 8.1 系统上用Oracle VM VirtualBox 安装windows 2008 R2 SP1 的虚拟机 出现 Error Code: 0x000000C4
Windows 8.1 本来可以安装Hyper-v来安装虚拟机,但是我现在需要使用Oracle VM VirtualBox来安装虚拟机, 所以必须先卸载Hyper-v VirtualBox 安装的虚拟 ...
- IOS开发-cell的动态高度
tableView中自定义cell的高度随子控件的内容动态变化,也是用的非常多的地方.现在就来处理一个自定义一个里面有文字(多少不定),图片(有无不定)的cell 首先要准备两个模型,一个是存放数据的 ...
- HackerRank "Chocolate in Box" !
XOR -> 0 is the key (make it even pair): http://www.cnblogs.com/lautsie/p/3908006.html Something ...
- LintCode "Submatrix Sum"
Naive solution is O(n^4). But on 1 certain dimension, naive O(n^2) can be O(n) by this well-known eq ...
- IntelliJ IDEA常用快捷键
双击shift 项目所有目录查找 ctrl+f 文件查找特定内容 ctrl+shift+f 项目查找包含特定内容的文件 ctrl+n 新建类 ctrl+shift+n 查找文件 ctrl+e 最近的 ...
- FTP主/被动模式的原理
---------------------------------------------------------------------------------------------------- ...
- xss攻击和sq注入
xss攻击跟SQL注入的原理还是挺简单的,都是利用web是使用字符串进行操作的原理,通过伪造分隔符或者结束符号,来让网页或者服务端来运行输入的代码 一般防御的方法就是在对一些分隔符进行转义,djang ...
- 【solr】solr5.0整合中文分词器
1.solr自带的分词器远远满足不了中文分词的需求,经查使用最多的分词器是solr是mmseg4j分词器,具体整合大家可以参考 https://github.com/zhuomingliang/mms ...