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的更多相关文章

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

  2. 【刷题-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 ...

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

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

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

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

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

  8. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  9. 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 ...

随机推荐

  1. oracle随笔(转)

    ---------数学函数 1.绝对值S:select abs(-1) valueO:select abs(-1) value from dual 2.取整(大)S:select ceiling(-1 ...

  2. 解决VS报表.rdl 显示乱码“小方块”问题

    报表在编辑状态显示文本显示小方块 如图 原因:字体格式是英文状态下. 解决:选中文本框,选择文本框属性,选择字体,字体改成宋体或微软雅黑.就可以了.

  3. CocoaPods的使用详解

    CocoaPods是什么 当我们开发 iOS 项目时候,会经常使用到第三方类库,并且会使用很多.大家的做法基本上都是到 GitHub 上下载一个一个的类库,然后导入到工程中,并且引入各种的类库,做各种 ...

  4. android API文档查询---context、toast、SharedPreferences

    /*查阅api ---context1.abstract AssetManager     getAssets() Returns an AssetManager instance for the a ...

  5. 洛谷 P1108 低价购买

    P1108 低价购买 标签 动态规划 难度 提高+/省选- 题目描述 "低价购买"这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:& ...

  6. shopnc 商城源码阅读笔记-缓存技术

    缓存方式 : 从 shopnc 的缓存驱动目录 /framework/cache里已有的实现类来看,shopnc支持以下5种缓存方式 apc Eaccelerator file memcache xc ...

  7. Java遍历所有网卡打印对应IP

    import java.util.Enumeration; import java.net.*; public class Test { /** * @param args */ public sta ...

  8. 用Unity的Animation播放Animator动画Clip

    简单的动画,其实不需要Animator动画状态机管理,用Animation播放效率更高,但可能由于历史遗留问题,或网上下载的第三方资源,得到的是Animator资源,可以在Clip的Debug试图下, ...

  9. 4、Hbase

    1).逻辑模型 Hbase 以表的形式存储数据,每个表由行和列组成,每个列属于一个特定的列族. 表中由行和列确定的存储单元称为一个元素,每个元素保存了同一份数据的多个版本,由时间戳来标识.行健是数据行 ...

  10. custom event in javascript and jquery

    javascript: // add a eventListener document.addEventListener("abc", function(){alert('this ...