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
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的更多相关文章
- Leetcode211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(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 ...
- leetcode面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...
- 【刷题-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 ...
- (*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 ...
- 211. Add and Search Word - Data structure design
题目: Design a data structure that supports the following two operations: void addWord(word) bool sear ...
- [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 ...
随机推荐
- hibernate一对一关联手动改表后No row with the given identifier exists:
articleId手动改了一个并不存在的值 把被控端的id改成存在的就好了
- ongl与Struts标签
一.ONGL OGNL 的全称是“Object-Graph Navigation Language”,即对象图导航语言,它是一种功能强大的开源表达式语言.使用这种表达式语言可以通过某种表达式语法存取 ...
- SpringMVC注解开发方式
环境准备 springmvc版本:spring3.2 需要spring3.2所有jar(一定包括spring-webmvc-3.2.0.RELEASE.jar 工程结构 配置前端控制器(web.xml ...
- PHP学习1.5-预定义超全局数组变量
1.PHP 预定义的超全局变量数组 特性: a.特殊的数组,操作方式没有区别 b.不用声明,php脚本中默认存在,因为在php中不用定义,所以在自定义变量是应避免和预定的全局变量同名 c.在全局范围内 ...
- 修改input标签输入样式
去掉input自带的边框: border-style:none;修改input输入的文字样式: input{ font-size: 24px; color:#5d6494; } 修改input框中占位 ...
- img标签下多余空白BUG解决方法
在进行页面的DIV CSS排版时,遇到IE6(当然有时Firefox下也会偶遇)浏览器中的图片元素img下出现多余空白的问题绝对是常见的 对于该问题的解决方法也是“见机行事”. 1.将图片转换为块级对 ...
- js封装ajax的方法
常用的ajax请求方法封装 /** * ajax请求的封装代码 */ function ajaxPost(url, params, cb) { $.ajax({ type : 'post', url ...
- Linux ifconfig 查看网络接口状态
Linux ifconfig 如果不接任何参数,就会输出当前网络接口的情况: [root@localhost ~]# Linux ifconfig eth0 Link encap:Ether ...
- Leetcode46. Permutations全排列
给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1 ...
- phpBOM头(字符)出现的原因以及解决方法_PHP程序员博客|高蒙个人博客
今天在项目中发现,客户端在使用ajax得到返回值时,无法匹配字符串.总是报错,打开页面接口发现,页面的头部出现了的字符(BOM头),找到问题了,那么直接用代码清除掉即可. php隐形字符 // 如 ...