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 ...
随机推荐
- SQL 用于各种数据库的数据类型
SQL 用于各种数据库的数据类型 Microsoft Access.MySQL 和 SQL Server 所使用的数据类型和范围. Microsoft Access 数据类型 数据类型 描述 存储 T ...
- 【LeetCode-面试算法经典-Java实现】【066-Plus One(加一)】
[066-Plus One(加一)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a non-negative number represented as ...
- react-document-title
根据不同的路由改变文档的title 使用该组件: import ReactDocumentTitle from 'path/ReactDocumentTitle' render() { return ...
- Unity 3D 中动态字体的创建
原创不易,转载请注明转自: http://blog.csdn.net/u012413679/article/details/26232453 ---- kosion 1.载入NGUI插件包,载入完毕后 ...
- 有两个字符串a,b。假设a="ab",b="cd",判断字符串c="acbd"是属于a、b的组合。满足组合后a、b的内部顺序均不变。
#include<iostream> #include<string> using namespace std; int check(string a,string b,str ...
- iptraf 网卡 ip 端口 监控 netstat 关闭端口方法
18 commands to monitor network bandwidth on Linux server – BinaryTides https://www.binarytides.com/l ...
- GET和POST 编码和乱码
1. 什么是URL编码. URL编码是一种浏览器用来打包表单输入的格式,浏览器从表单中获取所有的name和其对应的value,将他们以name/value编码方式作为URL的一部分或者分离的发送到服 ...
- C # 踩坑记录(20190603)
由于公司战略层需求,需要学习c#,在此仅记录相关问题,以便后期回顾. 学习路线 .NET 框架学习与C # 的关系 Visual Studio 简介及相关帮助网站(msdn) Main 方法及&quo ...
- 织梦发布的文章如何批量替换文章"来源"和"作者"?
做的网站中已经采集好并已生成HTML了的文章或以前已发布的文章如何快速批量替换所有“来源”和“作者”呢?第一步:打开:dede模板网站(后台目录)\templets\article_add.htm ( ...
- 为ios app添加广告条
1.广告简介 2.实现步骤: 1>.添加 iAd.framework 框架 2,使用storyboard 运行结果: 2>添加 ADBannerView 视图,并设置代理方法 3>思 ...