leetcode@ [211] Add and Search Word - Data structure design
https://leetcode.com/problems/add-and-search-word-data-structure-design/
本题是在Trie树进行dfs+backtracking操作。
Trie树模板代码见:http://www.cnblogs.com/fu11211129/p/4952255.html
题目介绍:
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
.
- struct Trie{
- Trie *next[]; //include character '.'
- bool isWord;
- Trie() {
- for(auto &n : this->next) n = NULL;
- this->isWord = false;
- }
- };
- class WordDictionary {
- public:
- Trie *root;
- WordDictionary() {
- this->root = new Trie();
- }
- void insert(string s) {
- Trie *p = this->root;
- for(auto &c: s) {
- int idx = c - 'a';
- if(!p->next[idx]) p->next[idx] = new Trie();
- p = p->next[idx];
- }
- p->isWord = true;
- }
- void addWord(string word) {
- insert(word);
- }
- bool dfs(Trie *p, string word, int idx) {
- if(idx == word.size()-) {
- if(word[idx] == '.') {
- for(int i=;i<;++i) {
- if(p->next[i] != NULL && p->next[i]->isWord) return true;
- }
- return false;
- }
- else {
- int nidx = word[idx] - 'a';
- if(p->next[nidx] == NULL) return false;
- else return p->next[nidx]->isWord;
- }
- }
- if(word[idx] == '.') {
- for(int i=;i<;++i) {
- if(p->next[i] != NULL && dfs(p->next[i], word, idx+)) return true;
- }
- }
- else {
- int nidx = word[idx] - 'a';
- if(! p->next[nidx]) return false;
- if(p->next[nidx] && dfs(p->next[nidx], word, idx+)) return true;
- }
- return false;
- }
- // 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) {
- bool flag = false;
- for(int i=;i<;++i) {
- if(root->next[i] != NULL) {
- flag = true; break;
- }
- }
- if(!flag) return false;
- return dfs(root, word, );
- }
- };
- // Your WordDictionary object will be instantiated and called as such:
- // WordDictionary wordDictionary;
- // wordDictionary.addWord("word");
- // wordDictionary.search("pattern");
leetcode@ [211] Add and Search Word - Data structure design的更多相关文章
- 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 ...
- (*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 ...
- [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 211. Add and Search Word - Data structure design Trie树
题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...
- [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 ...
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
随机推荐
- hdu 4403
水水的dfs #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath& ...
- 【leetcode】Combination Sum II (middle) ☆
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- URAL 1260 Nudnik Photographer(递推)
题目链接 题意 : 给你1到n这n个数,排成一排,然后1放在左边最开始,剩下的数进行排列,要求排列出来的数列必须满足任何两个相邻的数之间的差不能超过2,问你有多少种排列 思路 : 对于dp[n], n ...
- 【BZOJ 1038】 1038: [ZJOI2008]瞭望塔
1038: [ZJOI2008]瞭望塔 Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 ...
- Android 通过http访问服务器
目前Android 与服务器交互有两种方式:1.Socket 2. Http : 但由于Http的封装性以及性能比socket要好,所以推荐使用http方式和服务器交互: 通过http访问服务器有三种 ...
- Android:时间控件
1.选择时间TimePicker 监听器:OnTimeChangedListener(obj,int hour,int minute); 常用: 获取时:getCurrentHour(). 获取 ...
- SRM 586 DIV1 L1
可以化简为求n条线段的最大覆盖问题,需要注意的是对于实数而言. #include <iostream> #include <vector> #include <strin ...
- 关于C的一些理解
关于字符数组和字符指针 关于相互赋值问题一只有疑问,其实是自己搞不清指针和地址的关系.地址可以指向一块内存但是不一定存在于内存,比如字符数组名,数组名是地址,但是不实际存在于内存中,无法修改,而字符指 ...
- IPC是什么意思?
IPC(Inter-Process Communication,进程间通信)IPC ( Instruction per Clock 及CPU每一时钟周期内所执行的指令多少) IPC代表了一款处理器的设 ...
- Where is Vasya?
Where is Vasya? Vasya stands in line with number of people p (including Vasya), but he doesn't know ...