lintcode-473-单词的添加与查找
473-单词的添加与查找
设计一个包含下面两个操作的数据结构:addWord(word), search(word)
addWord(word)会在数据结构中添加一个单词。而search(word)则支持普通的单词查询或是只包含.和a-z的简易正则表达式的查询。
一个 . 可以代表一个任何的字母。注意事项
你可以假设所有的单词都只包含小写字母 a-z。
样例
addWord("bad")
addWord("dad")
addWord("mad")
search("pad") // return false
search("bad") // return true
search(".ad") // return true
search("b..") // return true标签
字典树
思路
使用字典树,如何实现一个字典树相关见 lintcode-442-实现 Trie
- addWord 对应字典树的插入操作
- search 对应字典树查询操作,不过需要注意 '.' (正则表达式)的操作,因为 '.' 可以对应任何字符,所以要遍历 '.' 对应节点的所有子树,而非像其他字符那样,遍历对应的一条子树就可以。且所有子树有一条符合既可以证明存在此单词,所以要将遍历结果执行 '或' 操作
code
/**
* Your Trie object will be instantiated and called as such:
* Trie trie;
* trie.insert("lintcode");
* trie.search("lint"); will return false
* trie.startsWith("lint"); will return true
*/
class TrieNode {
public:
// Initialize your data structure here.
char c;
TrieNode * next[26];
bool isEnd;
TrieNode() {
c = ' ';
for (int i = 0; i < 26; i++) {
next[i] = nullptr;
}
isEnd = false;
}
TrieNode(char c) {
this->c = c;
for (int i = 0; i < 26; i++) {
next[i] = nullptr;
}
isEnd = false;
}
};
class WordDictionary {
private:
TrieNode* root;
public:
WordDictionary() {
root = new TrieNode();
}
// Adds a word into the data structure.
void addWord(string word) {
// Write your code here
TrieNode* curNode = root;
for (int i = 0; i < word.size(); i++) {
if (curNode->next[word[i] - 'a'] != nullptr) {
curNode = curNode->next[word[i] - 'a'];
}
else {
TrieNode* node = new TrieNode(word[i]);
curNode->next[word[i] - 'a'] = node;
curNode = node;
}
}
curNode->isEnd = 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) {
// Write your code here
return search(word, root);
}
bool search(string word, TrieNode* root) {
if (word.size() == 0 && root != NULL) {
return root->isEnd;
}
else if (word[0] != '.' && root != NULL) {
return search(word.substr(1, word.size() - 1), root->next[word[0] - 'a']);
}
else if (word[0] == '.' && root != NULL) {
bool result = false;
for (int i = 0; i < 26; i++) {
if (root->next[i] != nullptr) {
result = result | search(word.substr(1, word.size() - 1), root->next[i]);
}
}
return result;
}
else if (root == NULL) {
return false;
}
}
};
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
lintcode-473-单词的添加与查找的更多相关文章
- 单词的添加与查找 · Add and Search Word
[抄题]: 设计一个包含下面两个操作的数据结构:addWord(word), search(word) addWord(word)会在数据结构中添加一个单词.而search(word)则支持普通的单词 ...
- python实现将字符串中以大写字母开头的单词前面添加“_”下划线
在工作中写测试用例代码生成的时候,函数命令考虑采用参数文件的名称来命名,但是发现文件命名是驼峰的写写法,所以想按照字符串中的大写字母做分割,每个单词前面添加下划线,主要考虑采用正则的模式来匹配,替换然 ...
- 关于eclipse添加自动查找文件以及svn的插件
1.添加自动查找当前文件位置的插件(如下图) 在百度搜索下载 OpenExplorer_1.5.0.v201108051313.jar,下载之后放入eclipse下面的plugin文件夹下面既可以 2 ...
- [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 添加和查找单词-数据结构设计
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 ...
- Lintcode---单词的添加与查找
设计一个包含下面两个操作的数据结构:addWord(word), search(word) addWord(word)会在数据结构中添加一个单词.而search(word)则支持普通的单词查询或是只包 ...
- 使用JAVA编写电话薄程序,具备添加,查找,删除等功能
//该程序需要连接数据库.根据word文档要求所有功能均已实现.//大部分方法基本差不多,//在查询修改的时候能输出 最大ID号 和最小ID号,并且可以对输入的ID号进行判断是否存在(具体方法请查看 ...
- Eclipse添加快速查找Dao中方法所对应的Mybatis XML映射SQL的插件
Dao关联Mybatis快速查找的插件安装地址:http://dl.bintray.com/harawata/eclipse 安装步骤: ①Eclipse ==> Help ==> Ins ...
随机推荐
- FLINK流计算拓扑任务代码分析<二>
首先 是 StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment(); 我们在编写 fl ...
- Mysql Explain的简单使用
Mysql Explain 主要重要的字段有上面红色方框圈出来的那几个. type: 连接类型,一个好的SQL语句至少要达到range级别,杜绝出现all级别. key: 使用到的索引名,如果没有选择 ...
- 读耗子叔的《从Equifax信息泄露看数据安全》
本文永久地址:https://www.cnblogs.com/erbiao/p/9214219.html 最近正好看到耗子叔<从Equifax信息泄露看数据安全>这篇文章,就来说一下这篇文 ...
- ie的盒模型和标准模型
使用 box-sizing:content-box || border-box || inherit 原理图 计算 怪异模型|IE模型 div宽度(定死) = 内容宽度+border宽度+paddin ...
- SQLSERVER字符串处理函数
sqlserver提供了一系列字符串处理函数:substring.left.right.len.charindex.patindex.replace.replicate.stuff.upper.low ...
- 证明SG中梯度的期望等于GD的梯度
参考链接: https://zhuanlan.zhihu.com/p/36435504
- 20155206 2016-2017-2 《Java程序设计》第1周学习总结
20155206 2016-2017-2 <Java程序设计>第1周学习总结 第一,二章学习内容总结 第一章 java基本知识 Java的三种技术架构: JAVAEE:Java Platf ...
- 20155304 2016-2017-2 《Java程序设计》第八周学习总结
20155304 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 NIO NIO使用频道来衔接数据节点,在处理数据时,NIO可以让你设定缓冲区容量,在缓冲区中 ...
- switchsharp
https://www.switchysharp.com/file/switchysharp-v1.10.4.zip
- OpenStack入门篇(八)之镜像服务Glance
一.Glance的概述 Glance是为虚拟机的创建提供镜像的服务,我们基于Openstack是构建基本的IaaS平台对外提供虚拟机,而虚拟机在创建时必须为选择需要安装的操作系统,Glance服务就是 ...