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
解题思路:

参考之前的Java for LeetCode 208 Implement Trie (Prefix Tree) 修改下即可,JAVA实现如下:

public class WordDictionary extends Trie {

	public void addWord(String word) {
super.insert(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 (word == null || word.length() == 0)
return false;
return search(word, 0, root);
} public boolean search(String word, int depth, TrieNode node) {
if (depth == word.length() - 1) {
if (word.charAt(depth) != '.') {
if (node.son[word.charAt(depth) - 'a'] != null) {
node = node.son[word.charAt(depth) - 'a'];
return node.isEnd;
} else
return false;
}
for (int i = 0; i < 26; i++) {
if (node.son[i] != null) {
TrieNode ason = node.son[i];
if (ason.isEnd)
return true;
}
}
return false;
}
if (word.charAt(depth) != '.') {
if (node.son[word.charAt(depth) - 'a'] != null) {
node = node.son[word.charAt(depth) - 'a'];
return search(word, depth + 1, node);
} else
return false;
}
for (int i = 0; i < 26; i++) {
if (node.son[i] != null) {
TrieNode ason = node.son[i];
if (search(word, depth + 1, ason))
return true;
}
}
return false;
}
} class TrieNode {
// Initialize your data structure here.
int num;// 有多少单词通过这个节点,即节点字符出现的次数
TrieNode[] son;// 所有的儿子节点
boolean isEnd;// 是不是最后一个节点
char val;// 节点的值 TrieNode() {
this.num = 1;
this.son = new TrieNode[26];
this.isEnd = false;
}
} class Trie {
protected TrieNode root; public Trie() {
root = new TrieNode();
} public void insert(String word) {
if (word == null || word.length() == 0)
return;
TrieNode node = this.root;
char[] letters = word.toCharArray();
for (int i = 0; i < word.length(); i++) {
int pos = letters[i] - 'a';
if (node.son[pos] == null) {
node.son[pos] = new TrieNode();
node.son[pos].val = letters[i];
} else {
node.son[pos].num++;
}
node = node.son[pos];
}
node.isEnd = true;
} // Returns if the word is in the trie.
public boolean search(String word) {
if (word == null || word.length() == 0) {
return false;
}
TrieNode node = root;
char[] letters = word.toCharArray();
for (int i = 0; i < word.length(); i++) {
int pos = letters[i] - 'a';
if (node.son[pos] != null) {
node = node.son[pos];
} else {
return false;
}
}
return node.isEnd;
} // Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
if (prefix == null || prefix.length() == 0) {
return false;
}
TrieNode node = root;
char[] letters = prefix.toCharArray();
for (int i = 0; i < prefix.length(); i++) {
int pos = letters[i] - 'a';
if (node.son[pos] != null) {
node = node.son[pos];
} else {
return false;
}
}
return true;
}
} // Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");

Java for LeetCode 211 Add and Search Word - Data structure design的更多相关文章

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

  2. (*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(w ...

  3. leetcode@ [211] Add and Search Word - Data structure design

    https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...

  4. leetcode 211. Add and Search Word - Data structure design Trie树

    题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...

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

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

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

  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】211. Add and Search Word - Data structure design

    Add and Search Word - Data structure design Design a data structure that supports the following two ...

  9. 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...

随机推荐

  1. [css]通过transform缩放邮件客户端h5页面

    摘要 最近一直在折腾邮件通知的东东,大概逻辑就是如果有新邮件,向收件人的app推送一条服务号消息,并且在单击该消息的时候,需要展示邮件的详情. 技术 这里是使用Exchange EWS API来实现的 ...

  2. 【10-25】OOP基础-飞机游戏知识点

    知识点 鼠标适配器类为抽象类,使用匿名子类实现鼠标事件的重写,创建一个鼠标适配器对象 添加鼠标事件监听器到JPanel对象实现鼠标的响应 创建定时器Timer对象,在定时器的任务列表方法schedul ...

  3. 一款符合当前主流审美的Swing外观(Look and Feel)_测试版发布

    [前言] 本文将展示的是一款J2SE平台Swing外观(Look and Feel)实现,目前给出的演示jar包仅供测试之用,主体工作已经完成,余下是兼容性测试和调整,附件中的演示jar包推荐运行于j ...

  4. ThreadStart和ParameterizedThreadStart区别

    ThreadStart: ThreadStart这个委托定义为void ThreadStart(),也就是说,所执行的方法不能有参数. ThreadStart threadStart=new Thre ...

  5. 工具介绍 - NimbleText

    非常实用的工具, 即使不是程序员也有必要掌握这个简单的小工具. 这个工具有桌面版和在线版两个版本. 桌面版地址: http://nimbletext.com/ 在线版地址: http://nimble ...

  6. 新手使用R的注意事项

    1.最好先设置工作目录 如: setwd(“D:/DataDig”) 注意不是”\”,是”/” 再读取数据,如: datafile = read.csv("./test.csv") ...

  7. 71-IO 流

    JAVA IO 流 一.概括 1.IO(INPUT OUTPUT)流 1.1 IO 流用来处理设备之间的数据传输 1.2JAVA 对数据的操作是通过流的方式 1.3 JAVA 用于操作流的对象都在 I ...

  8. Redis的一些坑

    转载请注明出处哈:http://carlosfu.iteye.com/blog/2254154 上上周和同事(龙哥)参加了360组织的互联网技术训练营第三期,美团网的DBA负责人侯军伟给大家介绍了美团 ...

  9. SEO 百度后台主动推送链接

    实践步骤,先用爬虫程序将本网站的所有连接爬取出来,再用python文件处理程序把爬虫来的东东整理成一行一个链接的文本格式.再用postman接口测试工具,使用post方式,将所有的链接post过去,这 ...

  10. phpcms万能字段如何使用php方法

    来自:http://www.tantengvip.com/2013/12/phpcms-php-function/ phpcms后台内容模块->模型管理->添加字段功能很强大,你只需在ph ...