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 ...
随机推荐
- echarts 地图 免费离线js,json包分享
最近,项目中需要用到地图,由于项目的特殊性,只能使用内网获取数据. 然而,echarts官网上的离线地图包(http://echarts.baidu.com/download-map.html)早在一 ...
- C语言简易三子棋
这是本人依据现学知识写的简易三子棋,也不是那么简洁明了,望大佬指点 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include&l ...
- 第三次预作业20155231邵煜楠:虚拟机上的Linux学习
java第三次预作业--虚拟机初体验(学习记录) 学习在笔记本上安装Linux操作系统 通过老师给予的官网链接,下载了VirtualBox-5.1.14-112924-win和Ubuntu-16.04 ...
- 20155301 《Java程序设计》实验一(Java开发环境的熟悉)实验报告
20155301 <Java程序设计>实验一(Java开发环境的熟悉)实验报告 一.实验内容及步骤 (一)使用JDK编译.运行简单的java程序 命令行下的程序开发 步骤:打开cmd,建立 ...
- PostgreSQL通过mysql_fdw访问MySQL数据库
Mysql与PostgreSQL的安装过程省略. 为简便起见,把MySQL和PostgreSQL都安装在一个机器上,然后在此机器上(准确地说是在PostgreSQL运行的机器上)安装mysql_fdw ...
- 【AHOI2013】差异
题面 题解 $ \because \sum_{1 \leq i < j \leq n} i + j = \frac{n(n-1)(n+1)}2 $ 所以只需求$\sum lcp(i,j)$即可. ...
- CF 1083 B. The Fair Nut and Strings
B. The Fair Nut and Strings 题目链接 题意: 在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多. 分析: 建出trie树,给定的两个字符串就 ...
- superset 安装测试,基于windows 和 centos7.x
1.刚开始在windows平台测试搭建,报各种问题,搭建可以参考官网https://superset.incubator.apache.org/installation.html#deeper-sql ...
- StringUtils工具类用法
/*1.字符串以prefix开始*/ StringUtils.startsWith("sssdf","");//结果是:true StringUtils.sta ...
- toString()方法简单分析
问题描述 今天在使用spotbugs代码走查时发现这样一个问题,如下, String[] myArray=new String[] {"1","2"," ...