设计一个支持以下两个操作的数据结构:
void addWord(word)
bool search(word)
search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。 . 意味着它可以代表任何一个字母。
例如:
addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true
注意事项:
你可以假设所有单词都是由小写字母 a-z 组成的。

详见:https://leetcode.com/problems/add-and-search-word-data-structure-design/description/

Java实现:

class TrieNode {
public TrieNode[] children;
public boolean isWord = false;
public TrieNode(){
children=new TrieNode[26];
}
} class WordDictionary {
private TrieNode root; /** Initialize your data structure here. */
public WordDictionary() {
root=new TrieNode();
} /** Adds a word into the data structure. */
public void addWord(String word) {
TrieNode node = root;
for (char ch: word.toCharArray()) {
if (node.children[ch-'a'] == null) {
node.children[ch-'a'] = new TrieNode();
}
node = node.children[ch-'a'];
}
node.isWord = 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 helper(word, 0, root);
} private boolean helper(String word, int start, TrieNode node) {
if (start == word.length()){
return node.isWord;
}
char ch = word.charAt(start);
if (ch == '.') {
for (int i = 0; i < 26; i++) {
if (node.children[i] != null && helper(word, start+1, node.children[i])) {
return true;
}
}
} else {
return node.children[ch-'a'] != null && helper(word, start+1, node.children[ch-'a']);
}
return false;
}
} /**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/

C++实现:

class TrieNode {
public:
TrieNode *next[26];
char c;
bool isWord;
TrieNode() : isWord(false) {
for (auto & c: next)
{
c=nullptr;
}
}
TrieNode(char _c):c(_c),isWord(false)
{
for(auto &c:next)
{
c=nullptr;
}
}
};
class WordDictionary {
public:
WordDictionary() {
root = new TrieNode();
} // Adds a word into the data structure.
void addWord(string word) {
TrieNode *p = root;
for (auto &c : word)
{
int i = c - 'a';
if (!p->next[i])
{
p->next[i] = new TrieNode(c);
}
p = p->next[i];
}
p->isWord = true;
} // Returns if the word is in the data structure. A word could
// contain the dot character '.' to represent any one letter.
bool search(string word) {
return searchWord(word, root, 0);
} bool searchWord(string &word, TrieNode *p, int i) {
if (i == word.size())
{
return p->isWord;
}
if (word[i] == '.')
{
for (auto &c : p->next)
{
if (c && searchWord(word, c, i + 1))
{
return true;
}
}
return false;
}
else
{
return p->next[word[i] - 'a'] && searchWord(word, p->next[word[i] - 'a'], i + 1);
}
} private:
TrieNode *root;
}; // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");

参考:https://www.cnblogs.com/grandyang/p/4507286.html

211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计的更多相关文章

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

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

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

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

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

    设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...

  4. [LeetCode] 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]211. 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. 【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 ...

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

  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. 多校训练hdu --Nice boat(线段树,都是泪)

    Nice boat Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total ...

  2. 【java项目实战】一步步教你使用MyEclipse搭建java Web项目开发环境(一)

    首先.在開始搭建MyEclipse的开发环境之前.还有三步工具的安装须要完毕,仅仅要在安装配置成功之后才干够进入以下的java Web项目开发环境的搭建. 1.安装工具 第一步,下载并安装JDK,到官 ...

  3. Linux —— 查找与替换

    Linux —— 查找与替换 文本查找: grep, egrep, fgrep        grep:根据基本正则表达式定义的模式搜索文档,并将符合模式的文本行显示出来        注意:搜索时属 ...

  4. wampserver64安装时出现计算机缺少MCVR110.DLL无法安装等

    在安装wamp完成后运行出现上述问题,是因为wamp版本与DLL不对称.下面给出 wamp64位下载地址 http://www.onlinedown.net/soft/118187.htm vcred ...

  5. React通过Ajax获取数据

    React 组件的数据可以通过 componentDidMount 方法中的 Ajax 来获取,当从服务端获取数据库可以将数据存储在 state 中,再用 this.setState 方法重新渲染 U ...

  6. 02-Swift学习笔记-元组类型

    02-Swift学习笔记-元组类型 元组类型由N个任意类型的数据组成(N>=0) 元组类型的数据称为"元素" eg var size = (x:100 , y:100) si ...

  7. 2016/3/26 连接数据库 网页中数据的增删改 add delete update addchuli updateChuLi test8 DBDA

    主页面 test8.php <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  8. 下面forward和redirect的描述,正确的是(ABCD)

    A:forward是服务器将控制权转交给内部服务器对象,由新的对象来全权负责响应用户的请求 B:执行forward时,浏览器不知道服务器所发送的内容从那里来,浏览器地址栏中还是原来的地址 C:执行re ...

  9. 使用scanf_s报错:0xC0000005: Access violation writing location 0x00000000

    在vs2010中写了一行scanf("%s",name); 调式时 提示warning , 提示修改为scanf()使用可能会存在不安全,建议使用scanf_s() 但是我修改成s ...

  10. Nyquist–Shannon sampling theorem 采样定理

    Nyquist–Shannon sampling theorem - Wikipedia https://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_s ...