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 ...
随机推荐
- glassfish 日志输出配置
asadmin set-log-levels javax.enterprise.system.tools.deployment=WARNING
- Android笔记之adb命令应用实例1(手机端与PC端socket通讯上)
Android端的代码: 布局文件:activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/ ...
- 判断对象是不是Vector
判断对象是不是数组可以用 var arr:Array = []; trace(arr is Array);//true var vec:Vector.<String> = new Vect ...
- 循环/loop 结构/structure
1.Shell loop 2.C++/CPlusPlus ①.std::for_each ②.for loop ③.Iterator library 3.Python Loop ①.Python.or ...
- QT宏 Q_OBJECT,explicit, QHostAddress, quint, emit
QT相關 一. 參考: 1.宏Q_OBJECT 二. explicit struct constrcution 三. QHostAddress Detailed Description: The QH ...
- 九度OJ 1104 整除问题
题目地址:http://ac.jobdu.com/problem.php?pid=1104 题目描述: 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除. 输入: 两个整数n(2 ...
- Linux中的安装神器--yum源安装
linux配置yum源 一.修改yum的配置文件 /etc/yum.repos.d/xxx.repo 1.进入yum配置文件目录 # cd /etc/yum.r ...
- jexus 启动失败 原因定位
现象: root@test:/usr/jexus/siteconf# /usr/jexus/jws restartRestarting ... Failure 定位步骤: 1.查看/usr/jexus ...
- windows下使用MinGW的调试工具gdb.exe调试C程序
1.编译源代码 C:MinGW\bin>gcc.exe -g -o program.exe program.c 编译选项上要加上“g”,这样生成的目标程序会含有调试内容,再用gdb调试的时候才能 ...
- CODEVS 1638 修复公路
题目描述 Description A地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车.政府派人修复这些公路. 给出A地区的村庄数N,和公路数M,公路是双向的.并告诉你每条公路的连着哪两个村庄, ...