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 ...
随机推荐
- 编译个性化的openwrt固件
基本流程是:下载openwrt源码(推荐attitude adjustment版本).执行feeds更新.make menuconfig(通过配置feed.conf.default和menuconfi ...
- 启用Win8(中文版/核心版)中被阉割的远程桌面服务端
Windows 8/8.1 标准版(中文版/核心版)中取消了远程桌面服务端,想通过远程连接到自己的电脑就很麻烦了,第三方远程桌面速度又不理想(如TeamViewer).通过以下方法可让系统恢复远程桌面 ...
- android adb:电池与电量
手机每个硬件的耗电量是不一样的!比如屏幕就是耗电大户!其它一些元件则耗电量非常小! 使用android dumpsys工具可以获取电池以及电量消耗信息! dumpsys工具:battery.batte ...
- Windows下查询进程、端口
PID --> 端口号netstat -ano | findstr 8244 端口号 --> PIDnetstat -aon|findstr "11211" PID - ...
- ZOJ 2624 Popo's Lamps(DP 记忆化搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2624 题目大意:popo要将给定数量的灯变成自己想要的颜色,有一 ...
- HTML5入门篇
---- HTML5简介 HTML5 是用于取代1999年所制定的 HTML 4.01 和 XHTML 1.0 标准的 HTML 标准版本,现在仍处于发展阶段,但大部分浏览器已经支持某些 HTML5 ...
- sea.js说明文档
Sea.js 手册与文档 首页 | 索引 目录 模块定义 define id dependencies factory exports require require.async require.re ...
- ubuntu的syslog为空,停止写入解决方法
修改syslog权限: chown syslog:adm syslog
- keepalived+haproxy-部署高可用负载均衡
环境: 准备两台机子,安装haproxy及keepalive都一样,只是配置不一样而已. 这里只说明一台机子上安装haproxy及keepalive. ======================== ...
- Python3 内建模块 hashlib、itertools、HTMLParser、urllib
Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制 ...