Lintcode---单词的添加与查找
设计一个包含下面两个操作的数据结构: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
思路:先定义字典树节点类,用以实现字典树;
添加单词过程和之前的过程一模一样,在查找的时候,过程也类似,但要对‘.’字符进行特殊处理,这是问题的关键;
因为字符为'.'的时候,一个 . 可以代表一个任何的字母,在这种情况下,使用递归比较好实现。
/*
思路:先定义字典树节点类,用以实现字典树; 添加单词过程和之前的过程一模一样,在查找的时候,过程也类似,但要对‘.’字符进行特殊处理,这是问题的关键; 因为字符为'.'的时候,一个 . 可以代表一个任何的字母,在这种情况下,使用递归比较好实现。 */ //节点类的定义,注意构造函数对所有数据成员都进行初始化; const int MAX_CHILD=26;
class TrieNode {
public:
// Initialize your data structure here.
int count;
TrieNode* child[MAX_CHILD];
TrieNode() {
for(int i = 0; i < 26; i++)
child[i] = NULL;
count=0;
}
}; class WordDictionary {
public: WordDictionary() {
root = new TrieNode();
} // Adds a word into the data structure.
//添加单词过程和插入过程一模一样
void addWord(string word) {
// Write your code here if(root==NULL||word.size()==0){
return;
} int len=word.size();
TrieNode* t=root;
int i=0; while(i<len){
if(t->child[word[i]-'a']==NULL){
TrieNode* temp=new TrieNode();
t->child[word[i]-'a']=temp;
t=t->child[word[i]-'a'];
}
else{
t=t->child[word[i]-'a'];
}
i++;
}
t->count=1;
} // 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
search(word, root, 0);
} bool search(string &word, TrieNode *p, int i){
if (i == word.size()){
return p->count;
} //当遇到字符为'.'的时候,一个 . 可以代表一个任何的字母;
//这里用递归的方式判断输入字符串是否存在; if (word[i] == '.') {
for (auto a : p->child) {
if (a && search(word, a, i + 1)){
return true;
}
}
return false;
}
else {
return p->child[word[i] - 'a'] && search(word, p->child[word[i] - 'a'], i + 1);
}
} private:
TrieNode *root;
}; // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
Lintcode---单词的添加与查找的更多相关文章
- 单词的添加与查找 · Add and Search Word
[抄题]: 设计一个包含下面两个操作的数据结构:addWord(word), search(word) addWord(word)会在数据结构中添加一个单词.而search(word)则支持普通的单词 ...
- lintcode-473-单词的添加与查找
473-单词的添加与查找 设计一个包含下面两个操作的数据结构: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:单词切分
单词切分 给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词. 样例 s = "lintcode" dict = ["lint&qu ...
- 使用JAVA编写电话薄程序,具备添加,查找,删除等功能
//该程序需要连接数据库.根据word文档要求所有功能均已实现.//大部分方法基本差不多,//在查询修改的时候能输出 最大ID号 和最小ID号,并且可以对输入的ID号进行判断是否存在(具体方法请查看 ...
随机推荐
- 在XC2440的uboot中挂载U盘,利用FAT文件系统读写U盘文件
转:http://blog.chinaunix.net/uid-22030783-id-3347608.html 在XC2440的uboot_V1.3版本中已经支持USB HOST驱动和FAT文件系统 ...
- jquery的一次点击实现
1.项目中需要实现一个需求,第一个点击的时候允许弹出dialog对象框,第二次不允许,除非重新刷新页面 2.在js的click事件中定义一个标签属性:相当于设置一个全局变量 var auclot= ' ...
- iOS:判断用户名是否以字母开头、手机号输入、邮箱是否正确的正则表达式
新建一个字符串分类:NSString(Check),定义类方法更方便 .h文件 #import <Foundation/Foundation.h> @interface NSString ...
- IIS漏洞过滤
IIS漏洞报告会提示网站HEAD包含Server版本信息导致版本泄漏,看起来不是大问题的漏洞却被分到中危或高危的行列中, 因为攻击者可能使用被披露信息获取特定版本发现的安全漏洞以及利用程序. 下面提供 ...
- HTML5基础知识汇总_(2)自己定义属性及表单新特性
自己定义属性data-* 说起这个属性,事实上如今非经常见了;怎么说呢,由于在一些框架都能看到他的身影!!! 比方Jquery mobile,里面非常频繁的使用了这个属性; 这个属性是哪里来的-.当然 ...
- vs2017 自定义生成规则 错误 MSB3721 命令 ”已退出,返回代码为 1。
错误 MSB3721 命令 ”已退出,返回代码为 1. 解决办法:去掉yasm复选框,改为masm vs2017 自定义生成规则-编译汇编代码 VC++调用yasm编译汇编代码有三种方法:Custom ...
- actor中!(tell)与forward的差别
! 的源代码: def !(message: Any)(implicit sender: ActorRef = Actor.noSender): Unit tell 的源代码: final def t ...
- python中的socket服务器(多线程)
最近在写一个客户端和服务器的项目,使用了SocketServer模块,网上大多数都是TCP连接的例子,我在这总结一个UDP. 直接贴上代码 import threadingimport SocketS ...
- org.eclipse.e4.core.di.InjectionException:org.eclipse.swt.SWTException: Widget is disposed
org.eclipse.e4.core.di.InjectionException:org.eclipse.swt.SWTException: Widget is disposed 开发环境为ecli ...
- EAS开发
WAFII中的 数据获取与传输 首先看实例代码: DataAction:function(){ //获取选中所有列的id var selectedIds = waf("#grid" ...