leetcode面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design
1 题目
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.
2 思路
该题是实现trie
树的变种。因此我们首先需要定义节点的数据结构。
TrieNode
节点的属性主要包含以下几点:
char content
:节点字符内容boolean isEnd
:节点是否为一个单词结束的标记LinkedList<TrieNode> childNode
: 该节点的所有的孩子节点
void addWord(String word)
和Trie
一样。
boolean search(String word)
采用dfs 来进行搜索。
3 代码
import java.util.LinkedList;
public class WordDictionary {
TrieNode root;
public WordDictionary() {
root = new TrieNode();
}
// Adds a word into the data structure.
public void addWord(String word) {
if (search(word)) {
return;
}
int len = word.length();
TrieNode cur = root;
for (int i = 0; i < len; i++) {
char c = word.charAt(i);
TrieNode tmp = cur.subNode(c);
if (tmp == null) {
tmp = new TrieNode(c);
cur.childNode.add(tmp);
cur = tmp;
} else {
cur = tmp;
}
}
cur.isEnd = 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 dfs(root, word, 0);
}
private boolean dfs(TrieNode root, String word, int start) {
int len = word.length();
if (start == len) {
if (root.isEnd)
return true;
else
return false;
}
char c = word.charAt(start);
if (c != '.') {
TrieNode tmp = root.subNode(c);
if (tmp == null)
return false;
else {
return dfs(tmp, word, start + 1);
}
} else { // '.'
for (TrieNode next : root.childNode) {
boolean found = dfs(next, word, start + 1);
if (found) {
return true;
}
}
}
return false;
}
static class TrieNode {
// Initialize your data structure here.
char content; // 节点内容
boolean isEnd;// 是否为一个单词的结尾
LinkedList<TrieNode> childNode;// 该节点所有的孩子节点
// Initialize your data structure here.
public TrieNode() {
this.content = 0;
this.isEnd = false;
this.childNode = new LinkedList<TrieNode>();
}
public TrieNode(char content) {
this.content = content;
this.isEnd = false;
this.childNode = new LinkedList<TrieNode>();
}
public TrieNode subNode(char c) {
for (TrieNode tn : childNode) {
if (tn.content == c) {
return tn;
}
}
return null;
}
}
public static void main(String[] args) {
WordDictionary wordDictionary = new WordDictionary();
wordDictionary.addWord("bad");
wordDictionary.addWord("dad");
wordDictionary.addWord("add");
boolean res = wordDictionary.search(".ad");
System.out.println(res);
}
}
// Your WordDictionary object will be instantiated and called as such:
4 总结
很不错的问题,估计面试网易有道,360,百度这样的企业会考到。
leetcode面试准备:Add and Search Word - Data structure design的更多相关文章
- 【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,搜索单词,前缀树,字典树 ...
- [leetcode trie]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 OJ: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)空间复杂度:字典树远远小于哈 ...
- 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 ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- 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 ...
随机推荐
- 在页面生命周期执行时 Page 对象在 SaveState 阶段都发生了什么事?
先看下 SaveViewState 的源码: // Answer any state this control or its descendants want to save on freeze. / ...
- 添加数据库的Maven依赖(SqlServer,Oracle)
oracle: 1.在Oracle官网下载ojdbc的jar包 例:ojdbc7.jar,版本是12.1.0.2,存储地址/home/peng/下载 2.dos中进入存储地址执行如下命令行(注意各项对 ...
- JS中undefined和null的区别
在写JS脚本的时候,经常会碰到“为空”的判断,其中主要有null和undefined的判断.这两个为空判断的主要区别是: 1) null是JS的关键字,是语法特性.undefined是全局对象的属性, ...
- linux系统 备份与还原
linux 系统备份与还原备份系统:1.成为 root 用户: su root2.进入根目录: cd /3.用tar命令打包压缩:tar cvpjf 压缩包名.tar.bz2 --exclude=/压 ...
- 、Dll文件的编写 调用 说明
1>新建Dll文件TestLib.dll 新建Unit文件U_TestFunc U_TestFunc代码如下: unit U_TestFunc; interface uses //尽可能的少us ...
- HBase的安装与使用
1.安装 由于还是学习阶段,所以没有在生产环境练习,就在本地建了个虚拟机进行HBase的安装. 下载地址http://www.apache.org/dyn/closer.cgi/hbase/,选择一个 ...
- ASP.NET MVC Spring.NET NHibernate 整合
请注明转载地址:http://www.cnblogs.com/arhat 在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做.同理这个三个技术也是.那么在整合之前,需 ...
- 关于xcode6打包以及上线前企业部署测试的说明 --转自张诚教授微博
xcode6如何打包 首先clean然后点击归档 点击打包之后保存 点选第一个以后检查相关证书签名 那么我们开发完以后,在上线前如何给别人测试 有2种方法 1.使用299美金的企业开发者账号搭建企业部 ...
- 服务器环境搭建系列(二)-Tomcat篇
1.解压缩Tomcat的tar包,默认放在opt下 tar -zxvf apache-tomcat-6.0.35.tar.gz 2.输入如下命令修改tomcat配置文件 vi /opt/apache- ...
- sublimtest3文件名乱码问题及解决方案
在sublime text 3中,Preference, Settings-User,最后加上一行"dpi_scale": 1.0覆盖操作系统设置的DPI. 这是我的Setting ...