211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两个操作的数据结构:
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 添加与搜索单词 - 数据结构设计的更多相关文章
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
- [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 ...
- Leetcode211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [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 ...
- 字典树(查找树) 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】211. Add and Search Word - Data structure design
Add and Search Word - Data structure design Design a data structure that supports the following two ...
- 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 ...
随机推荐
- Visual Studio VS如何重置所有设置
工具-导入和导出设置-重置所有设置,点击下一步即可.
- Oracle 模糊查询方法
在这个信息量剧增的时代,怎样帮助用户从海量数据中检索到想要的数据.模糊查询是不可缺少的. 那么在Oracle中模糊查询是怎样实现的呢? 一.我们能够在where子句中使用likeke ...
- 李洪强iOS开发之函数式 编程初窥
函数式 编程初窥 最近在学习Erlang和Python.Erlang是完全的函数式编程语言,Python语言是面向对象的语言,但是它的语法引入了大量的函数式编程思想.越研究越觉得函数式的编程思路可 ...
- PHP使用debug_backtrace方法跟踪代码调用
在开发过程中,例如要修改别人开发的代码或调试出问题的代码,需要对代码流程一步步去跟踪,找到出问题的地方进行修改.如果有一个方法可以获取到某段代码是被哪个方法调用,并能一直回溯到最开始调用的地方(包括调 ...
- Sql数据库查询语言
1.概述 Sql是一种面向数据库的结构化查询语言.是符合美国国家标准化组织ANSI的一种计算机标准语言. Sql具对数据库的操作有:增删改查.创建数据库.创建表.创建存储过程.创建视图等 RDBMS关 ...
- 大括号对struct进行初始化
1 partial initialization 即所谓的部分初始化. 这个时候,无论该struct变量是static的还是automic的,未显式初始化的成员都会被初始化为默认值.
- php判断手机号码
//PHP判断手机号码 public function isMobile($params) { $pattern = "/^(13[0-9]|14[0-9]|15[0-9]|17[0-9 ...
- 移动端和PC端有什么区别
1.PC考虑的是浏览器的兼容性,而移动端开发考虑的更多的是手机兼容性,因为目前不管是android手机还是ios手机,一般浏览器使用的都是webkit内核,所以说做移动端开发,更多考虑的应该是手机分辨 ...
- POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-2112 Optimal Milking Time Limit: 2000MS Memory Limit: 30000K T ...
- HDU3394 Railway —— 点双联通分量 + 桥(割边)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3394 Railway Time Limit: 2000/1000 MS (Java/Others) ...