[LintCode] Add and Search Word 添加和查找单词
Design a data structure that supports the following two operations: addWord(word) and 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.
Notice
You may assume that all words are consist of lowercase letters a-z.
Example
addWord("bad")
addWord("dad")
addWord("mad")
search("pad") // return false
search("bad") // return true
search(".ad") // return true
search("b..") // return true
LeetCode上的原题,请参见我之前的博客Add and Search Word - Data structure design。
class WordDictionary {
public:
struct TrieNode {
bool isLeaf;
TrieNode *child[];
};
WordDictionary() {
root = new TrieNode();
}
// Adds a word into the data structure.
void addWord(string word) {
TrieNode *p = root;
for (char c : word) {
int i = c - 'a';
if (!p->child[i]) p->child[i] = new TrieNode();
p = p->child[i];
}
p->isLeaf = 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) {
search(word, root, );
}
bool search(string &word, TrieNode *p, int i) {
if (i == word.size()) return p->isLeaf;
if (word[i] == '.') {
for (auto a : p->child) {
if (a && search(word, a, i + )) return true;
}
return false;
} else {
return p->child[word[i] - 'a'] && search(word, p->child[word[i] - 'a'], i + );
}
}
private:
TrieNode *root;
};
[LintCode] Add and Search Word 添加和查找单词的更多相关文章
- 单词的添加与查找 · Add and Search Word
[抄题]: 设计一个包含下面两个操作的数据结构:addWord(word), search(word) addWord(word)会在数据结构中添加一个单词.而search(word)则支持普通的单词 ...
- 字典树(查找树) 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 ...
- 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] 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 ...
随机推荐
- Mysql数据库操作系统及配置参数优化
数据库结构优化 表的水平拆分常用的水平拆分方法为:1.对 customer_id进行 hash运算,如果要拆分成5个表 则使用mod(customer_id,5)取出0-4个值2.针对不同的 hash ...
- OK6410移植madplay播放器,王明学learn
对于ok6410的madplay移植主要包括三部分.声卡驱动移植,播放器的移植,以及alsa库的移植. 一.首先移植声卡驱动以及播放器 ok6410采用WM97系列的声卡芯片,要使得内核支持该驱动,首 ...
- OC对象的动态和静态构造区别
Student.h: #import <Foundation/Foundation.h> @interface Student : NSObject @property(nonatomic ...
- Linux将文件拷到u盘
1:终端中获取root后,使用fdisk -l来查看位置 2:挂载u盘:mount /路径 /mnt cd /mnt可进入u盘,ls可查看文件 3:复制:cp /(文件路径) /mnt 4:退出u盘: ...
- light oj 1422 Halloween Costumes (区间dp)
题目链接:http://vjudge.net/contest/141291#problem/D 题意:有n个地方,每个地方要穿一种衣服,衣服可以嵌套穿,一旦脱下的衣服不能再穿,除非穿同样的一件新的,问 ...
- poj2533 LIS
题目链接: http://poj.org/problem?id=2533 题意:第一个数n,接下来n个数,> ....求最长上升子序列. 这道题有两种解法,第一种是通解,也适用于别的LIS. ...
- express-17 持久化
简介 所有网站和Web应用程序(除了最简单的)都需要某种持久化方式,即某种比易失性内存更持久的数据存储方式,这样当遇到服务器宕机.断电.升级和迁移等情况时数据才能保存下来. 文件系统持久化 实现持久化 ...
- BZOJ 2631 Tree ——Link-Cut Tree
[题目分析] 又一道LCT的题目,LCT只能维护链上的信息. [代码] #include <cstdio> #include <cstring> #include <cs ...
- kafka 集群安装与安装测试
一.集群安装 1. Kafka下载:wget https://archive.apache.org/dist/kafka/0.8.1/kafka_2.9.2-0.8.1.tgz 解压 tar zxvf ...
- mac 快捷键大全
1.control+space 可以使用 spotlight搜索,用于快速找到所需要的文件 2.我尝试使用android studio 提示的快捷键来进行写代码,发现自己按照它的提示操作没有成功,原因 ...