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 ...
随机推荐
- 使用netbeans 搭建 JSF+SPRING 框架
spring版本使用4,jsf版本2.2 jsf的配置文件faces-config.xml <?xml version='1.0' encoding='UTF-8'?> <faces ...
- 从CSDN转战于此
平时喜欢写博客,开始是再CSDN,但CSDN博客功能实在不是很好.听说博客园的博客功能很不错,今天就来到此地.实际上很早就注册了博客园,但博客今天才申请的. 博客园,来了!
- shell小程序
因此需要挑选学生,因此需要一个抓阄的程序:要求:1.执行脚本后,想去的同学输入英文名字全拼,产生随机数01-99之间的数字,数字越大就去参加项目实践,前面已经抓到的数字,下次不能在出现相同数字.2.第 ...
- java_object的具体使用--上帝
就我们所知道的,java中有子类和父类,子类由于继承父类而形成,那么父类还有没有父类呢?答案是有了,父类的父类就是object类,一切父类都继承了它,那么根据继承的属性,每一个子类都有一个object ...
- vmware以及schlumberger题解
先是vmare的:具体的题目我就不描述了. 1. 贪吃的小明.直接数个数,统计个数,就可以完成.使用map,应该输入implement这一类,我认为很简单,但是我只过了33%. /* ID: y119 ...
- CICS定时
F+14个0-----给CICS传参数FM CICSServer svr; svr.GetRetCommData(&pMsgStr, &chMsgType); CICSS ...
- 6.5 k个已排好序链表合并为一个排序链表
1 建立链表(带哨兵位的)2 建立最小堆方法3 合并已排好序的k个链表 typedef int DataType; //建立链表 class Link { private: struct Node { ...
- 九度OJ 1056--最大公约数 1439--Least Common Multiple 【辗转相除法】
题目地址:http://ac.jobdu.com/problem.php?pid=1056 题目描述: 输入两个正整数,求其最大公约数. 输入: 测试数据有多组,每组输入两个正整数. 输出: 对于每组 ...
- 【nodemailer】 初试
nodemailer 是什么? 简单的讲nodemailer就是用来发送邮件的.最近的一个项目需要向客户的注册邮箱发送验证连接,研究了一下. 刚开始我以为nodemailer还可以用来接收邮件,看了好 ...
- 网络编程之UDP协议
UDP协议 UDP(User Datagram Protocol)也就是用户数据报协议,是一种无连接的传输层协议,提供面向事务的简单不可靠信息传送服务,IETF RFC 768是UDP的正式规范. 提 ...