设计一个包含下面两个操作的数据结构:addWord(word)search(word)

addWord(word)会在数据结构中添加一个单词。而search(word)则支持普通的单词查询或是只包含.a-z的简易正则表达式的查询。

一个 . 可以代表一个任何的字母。

注意事项

你可以假设所有的单词都只包含小写字母 a-z。

您在真实的面试中是否遇到过这个题?

Yes
样例

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---单词的添加与查找的更多相关文章

  1. 单词的添加与查找 · Add and Search Word

    [抄题]: 设计一个包含下面两个操作的数据结构:addWord(word), search(word) addWord(word)会在数据结构中添加一个单词.而search(word)则支持普通的单词 ...

  2. lintcode-473-单词的添加与查找

    473-单词的添加与查找 设计一个包含下面两个操作的数据结构:addWord(word), search(word) addWord(word)会在数据结构中添加一个单词.而search(word)则 ...

  3. python实现将字符串中以大写字母开头的单词前面添加“_”下划线

    在工作中写测试用例代码生成的时候,函数命令考虑采用参数文件的名称来命名,但是发现文件命名是驼峰的写写法,所以想按照字符串中的大写字母做分割,每个单词前面添加下划线,主要考虑采用正则的模式来匹配,替换然 ...

  4. 关于eclipse添加自动查找文件以及svn的插件

    1.添加自动查找当前文件位置的插件(如下图) 在百度搜索下载 OpenExplorer_1.5.0.v201108051313.jar,下载之后放入eclipse下面的plugin文件夹下面既可以 2 ...

  5. [LintCode] Add and Search Word 添加和查找单词

    Design a data structure that supports the following two operations: addWord(word) and search(word) s ...

  6. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  7. [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 ...

  8. lintcode:单词切分

    单词切分 给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词. 样例 s = "lintcode" dict = ["lint&qu ...

  9. 使用JAVA编写电话薄程序,具备添加,查找,删除等功能

    //该程序需要连接数据库.根据word文档要求所有功能均已实现.//大部分方法基本差不多,//在查询修改的时候能输出 最大ID号 和最小ID号,并且可以对输入的ID号进行判断是否存在(具体方法请查看 ...

随机推荐

  1. 大内密探HMM(转)

    源地址:http://blog.csdn.net/ppn029012/article/details/8923501 1. 赌场风云(背景介绍) 最近一个赌场的老板发现生意不畅,于是派出手下去赌场张望 ...

  2. PYTHON之爬虫学习(一)基础

    关于python爬虫,大家都很熟悉,那么我就不多说,开始做了. 首先,python爬虫先安装python库,主要是requests库,在windows中cmd中输入,pip install reque ...

  3. iOS 系统原生分享图片 文字 音乐 纯视频 网页

    为了方便使用,我封装了一个分享的工具类LFSystemShareUtil.工程要引Social.framework. LFSystemShareUtil.h #import <Foundatio ...

  4. MYSQL复习笔记3-用户和安全

    Date: 20140115Auth: Jin参考:http://dev.mysql.com/doc/refman/5.1/en/security.html 一.权限系统实现方式相关权限信息存储在几个 ...

  5. JavaScript 巧学巧用

    关于 微信公众号:前端呼啦圈(Love-FED) 我的博客:劳卜的博客 知乎专栏:前端呼啦圈 前言 由于工作和生活上的一些变化,最近写文章的频率有点下降了,实在不好意思,不过相信不久就会慢慢恢复过来, ...

  6. minishift的本地代码构建

    看文档说支持,但实际尝试了下,失败 发现仍然是找github中的代码 找了半天,发现是在目录下有.git的隐含目录,然后copy到其他目录下删除,然后再试,发现仍然失败. 日志看是指定的目录并没有传入 ...

  7. iOS:使用block代码块实现事件处理过程中的回调

    block是什么,这里就不多加强调了,它的优点: 第一:执行效率高,速度快 第二:使用起来比代理简单,省却不少代码,增强代码美感 有一些小的知识点要强调一下: 第一点:它类似于一个匿名函数,也跟jav ...

  8. QT5.11下载与安装教程

    一.QT软件下载 Qt 5.9 之后的安装包与之前相比,不再区分 VS 版本和 MinGW 版本,而是全都整合到了一个安装包中.因此,与之前的安装包相比,体积也是大了不少,以前是 1G 多,现在是 2 ...

  9. 模糊搜索:concat各种函数详解、like操作符、通配符

    if(StringUtils.isNotBlank(queryBean.getConditions())){ hqlBuilder.addWhereClause(" concat(this. ...

  10. Hive不等值连接

    select * from ( select t1.instalment_id as r_id , t2.instalment_id as p_id from (select instalment_i ...