字典树(Trie树相关)

208. Implement Trie (Prefix Tree)

Implement a trie with insertsearch, and startsWith methods. (Medium)

Note:
You may assume that all inputs are consist of lowercase letters a-z.

分析:

字典树即前缀匹配树,在空间不是很影响的情况下一般采用如下数据结构存储Trie节点

 class TrieNode {
public:
// Initialize your data structure here.
bool isWord;
TrieNode* child[];
TrieNode(): isWord(false){
memset(child, NULL, sizeof(child));
}
};

所以插入、查找等操作比较直观,直接见程序。

代码:

 class TrieNode {
public:
// Initialize your data structure here.
bool isWord;
TrieNode* child[];
TrieNode(): isWord(false){
memset(child, NULL, sizeof(child));
}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
TrieNode* r = root;
for (int i = ; i < word.size(); ++i) {
if (r -> child[word[i] - 'a'] == NULL) {
r -> child[word[i] - 'a'] = new TrieNode(); }
r = r -> child[word[i] - 'a'];
}
r -> isWord = true;
} // Returns if the word is in the trie.
bool search(string word) {
TrieNode* r = root;
for (int i = ; i < word.size(); ++i) {
if (r -> child[word[i] - 'a'] == NULL) {
return false; }
r = r -> child[word[i] - 'a'];
}
if (r -> isWord) {
return true;
}
return false;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode* r = root;
for (int i = ; i < prefix.size(); ++i) {
if (r -> child[prefix[i] - 'a'] == NULL) {
return false; }
r = r -> child[prefix[i] - 'a'];
}
return true;
} private:
TrieNode* root;
}; // Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");

211. Add and Search Word - Data structure design

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. (Medium)

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.

分析:

继续沿用字典树的思路,只不过加入了“.”通配符,所以在查找过程中加入DFS即可,用辅助函数helper

代码:

 class TrieNode {
public:
// Initialize your data structure here.
bool isWord;
TrieNode* child[];
TrieNode(): isWord(false){
memset(child, NULL, sizeof(child));
}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
TrieNode* r = root;
for (int i = ; i < word.size(); ++i) {
if (r -> child[word[i] - 'a'] == NULL) {
r -> child[word[i] - 'a'] = new TrieNode(); }
r = r -> child[word[i] - 'a'];
}
r -> isWord = true;
} //helper function for search, use to solve "." problem
bool helper(string word, int pos, TrieNode* curRoot) {
if (pos == word.size() && curRoot -> isWord) {
return true;
}
if (word[pos] != '.') {
if (curRoot -> child[word[pos] - 'a'] == NULL) {
return false;
}
else {
return helper(word, pos + , curRoot -> child[word[pos] - 'a']);
}
}
else {
bool flag = false;
for (int i = ; i < ; ++i) {
if (curRoot -> child[i] != NULL && helper(word, pos + , curRoot -> child[i]) ) {
flag = true;
}
}
return flag;
}
return true;
}
// Returns if the word is in the trie.
bool search(string word) {
return helper(word, , root);
} private:
TrieNode* root;
}; class WordDictionary {
public:
Trie dic;
// Adds a word into the data structure.
void addWord(string word) {
dic.insert(word);
} // 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 dic.search(word);
}
}; // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
 

LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design的更多相关文章

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

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

  2. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

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

  4. leetcode面试准备:Add and Search Word - Data structure design

    leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...

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

  6. (*medium)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 ...

  7. 211. Add and Search Word - Data structure design

    题目: Design a data structure that supports the following two operations: void addWord(word) bool sear ...

  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. 逻辑备份(mysqldump/select into outfile)

    #mysqldump备份 shell> mysqldump -uroot -p -P4306 sakila actor>E:\sakila-actor.sql shell> mysq ...

  2. Redis生存时间、删除策略和排序

    生存时间 设置命令 expire key long:设置数据在long秒后过期. pexpire key long:设置数据在long毫秒后过期. ttl key:查询数据剩余的生存时间.如果数据已过 ...

  3. hbase连接linux开发过程

    最近近公司被安排做hbase开发,太久没做今天记录下过程 import java.io.IOException; import org.apache.hadoop.conf.Configuration ...

  4. 『Power AI by AI』 PAI-AutoML2.0重磅发布

    PAI-AutoML调参服务是通过算法的方式解放用户调节算法参数的工作.自2018年8月发布PAI-AutoML1.0版本以来,该功能已经帮助众多PAI的中小企业用户提升了模型的准确性,得到了不错的反 ...

  5. js 高亮显示关键字

    示例: var defaultEmphasisHandler = function(keyword, data){ var regex = RegExp("("+keyword.r ...

  6. g++编译多个源原文件和头文件(转载)

    (头文件名是:Sales_item.hpp 源文件名:7-31.cpp和Sales_item.cpp)(点击我下载源文件) 方法1: 我用的命令是:g++ -o 7-31 7-31.cpp Sales ...

  7. jeecms使用小结

    前言: 使用jeecmsV9已经有一段时间,现在PC端的二次开发基本进入尾声,手机端的开发即将开始 ,由于项目时间比较紧,开发时不是每个人都会使用它自带的标签,所以在PC端开发的时候浪费了大量时间,为 ...

  8. Flask中的session机制

    cookie和sessioncookie:网站中,http请求是无状态的,第一次和服务器连接后并且登陆成功后,第二次请求服务器依然不能知道当前请求是哪个用户.cookie的出现就是解决了改问题,第一次 ...

  9. 数据库完整性 ch.5

    数据库的完整性 是指 数据的正确性(correctness) 和 相容性 (compat-ability) 5.1 实体完整性 定义 对单属性码的说明有两种方法,一种是定义为表约束条件,一种是定义为列 ...

  10. Leetcode605.Can Place Flowers种花问题

    假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. 给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花 ...