Add and Search Word
Trie 树的一个应用
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.
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
.
class TrieNode{
public:
TrieNode* child[];
bool isWord = false;
TrieNode() : isWord(false){
for(auto &a : child)
a = nullptr;
} }; class WordDictionary{
private:
TrieNode* root = new TrieNode();
public:
void addWord(string word){
TrieNode* p = root;
for(auto &a : word){
int i = a - 'a';
if(p->child[i] == nullptr) p->child[i] = new TrieNode();
p = p->child[i];
}
p->isWord = true;
} bool search(string word){
int n = word.length();
return search(word,n,,root);
} bool search(string& word,int n,int pos,TrieNode* cur){
if(pos == n) return cur->isWord;
if(cur == nullptr) return false; if(word[pos] == '.'){
for(int i=;i<;i++){
if(cur->child[i]){
if(search(word,n,pos+,cur->child[i])){
return true;
}
}
}
}else{
int i = word[pos] - 'a';
if(cur->child[i])
return search(word,n,pos+,cur->child[i]);
}
return false;
}
};
Add and Search Word的更多相关文章
- [LintCode] Add and Search Word 添加和查找单词
Design a data structure that supports the following two operations: addWord(word) and search(word) s ...
- leetcode面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...
- (Data structure)Implement Trie && Add and Search Word
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
- 【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 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- 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 ...
- 【刷题-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 ...
- [Swift]LeetCode211. 添加与搜索单词 - 数据结构设计 | Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
随机推荐
- textbox button 模拟fileupload
方案一: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.asp ...
- vector algorithm find
本来想着申请了博客园以后 我要写的博客都必须是有深度有内涵的...好吧 结果我只能说我想多了 还是得一步一步慢慢来 最近小学期的任务是要做一个学校食堂餐卡管理系统 有“严重拖延症”的我 果然 ...
- JavaScript 跨域小总结
一. 什么是跨域 域名分为:一级域名.二级域名.三级域名.例如:baidu.com(一级域名) .www.baidu.com(二级域名)tieba.baidu.com(二级域名).bbs.youa.b ...
- gucci fake bags is usually really a sign of luxurious
As soon as the violent trembling from the planet, standing company, people will certainly need to st ...
- $(document).ready() 与 window.onload 之间的区别
1.执行时机 window.onload 是网页中所有的元素都加载到浏览器后才执行 $(document).ready() 是dom完全就续就可以调用 例如:如果给一副图片添加点击事件,window. ...
- Devexpress DateEdit控件的值不反馈到数据源的处理方式。
如果在GridControl中要把编辑的值反馈到数据源,可以用Gridview1.PostEdit()方法. 可是在datalayout中使用就会遇到一些问题:比如说DateEdit控件,在保存数据的 ...
- Android AsyncTask异步任务(二)
之前我们讲过了AsyncTask 的生命周期(onPreExecute-->doInBackground-->onProgressUpdate-->onPostExecute),今天 ...
- hard
硬盘电路板将信号转化为电压高低,电压控制电脉冲被送到磁头,产生一个电磁.磁盘和磁头很紧密.读写都是不断扫描磁盘的过程.有操作系统负责控制文件的组织,可能在不同的块中.
- .net core 学习笔记(4)-ViewComponent
动态菜单,以前用的是Html.Action(url)来获取的,到了 .net core 中忽然发现没有了这个方法,原来在 .net core 中是提供了个 ViewComponent,有点类似以前的用 ...
- Android Studio 常用快捷键
继承Eclipse的快捷键 : File->Settings->Keymap->有一个Keymaps 下拉菜单,选择Eclipse. 这里讲主要常用的快捷键 : Ctrl ...