(*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,搜索单词,前缀树,字典树 ...
随机推荐
- dwr使用步骤
DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开发人员开发包含AJAX技术的网站.它可以允许在浏览器里的代码使用运行在 ...
- html之head,base,meta,title
一个简单的HTML最基本的必须的元素 用于定义文档的头部,是所有头部元素的容器.头部描述了文档的各种属性和信息,绝大多数头部的数据都不会直接显示给读者. 下面这些标签可用在head部分:base,li ...
- android 为应用程序创建桌面快捷方式技巧分享
手机装的软件过多,找起来很不方便,所以在主页面有一个快捷方式的话会很不错的,本文将介绍如何实现,需要了解跟多的朋友可以参考下 我们开发一款软件后,如果手机装的软件过多,去翻的话会很难翻的,所以 ...
- 【linux】linux如何进入单人维护模式修改root密码
- 【MySQL】索引长度的一些限制
有同学问到InnoDB的索引长度问题,简单说几个tips. MySQL的每个单表中所创建的索引长度是有限制的,且对不同存储引擎下的表有不同的限制. myisam表,单列索引,最大长度不能超过 1000 ...
- shell下root用户切换其他用户运行程序
工作中,一些程序,需要随机启动,但是不是以root用户运行,于是需要在rc.local中通过shell,从root用户切换到其他用户运行程序,命令如下: su -c 'command' - user ...
- c# 甘蔗斗地主1.4存档修改器
using System; using System.Collections.Generic; using System.ComponentModel; using System.Dat ...
- Tomcat插件与Jetty插件在MyEclipse中的配置
-Djetty.port=8101 jetty:run tomcat6:run <plugin> <groupId>org.apache.tomcat.maven</gr ...
- android的m、mm、mmm编译命令
android的m.mm.mmm编译命令的使用 android源码目录下的build/envsetup.sh文件,描述编译的命令 - m: Makes from the top of th ...
- C++多态的实现及原理详细解析
C++多态的实现及原理详细解析 作者: 字体:[增加 减小] 类型:转载 C++的多态性用一句话概括就是:在基类的函数前加上virtual关键字,在派生类中重写该函数,运行时将会根据对象的实际类型 ...