题目链接

写一个数据结构, 支持两种操作。 加入一个字符串, 查找一个字符串是否存在。查找的时候, '.'可以代表任意一个字符。

显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可以。 具体dfs方法看代码。

struct node
{node *next[26];int idEnd;node() {memset(next, NULL, sizeof(next));idEnd = 0;}
};
class WordDictionary {
public:// Adds a word into the data structure.node *root = new node();void addWord(string word) {node *p = root;int len = word.size();for(int i = 0; i<len; i++) {if(p->next[word[i]-'a'] == NULL)p->next[word[i]-'a'] = new node();p = p->next[word[i]-'a'];}p->idEnd = 1;return ;}int dfs(string word, int pos, node *p) {            //pos是代表当前是在哪一位。if(pos == word.size())return p->idEnd;int len = word.size();for(int i = pos; i<len; i++) {if(word[i] == '.') {for(int j = 0; j<26; j++) {if(p->next[j] == NULL)continue;if(dfs(word, pos+1, p->next[j]))return 1;}return 0;} else if(p->next[word[i]-'a'] != NULL) {return dfs(word, pos+1, p->next[word[i]-'a']);} else {return 0;}}}// 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) {return dfs(word, 0, root);}
};

转载于:https://www.cnblogs.com/yohaha/p/5119997.html

leetcode 211. Add and Search Word - Data structure design Trie树相关推荐

  1. LeetCode Add and Search Word - Data structure design(字典树)

    问题:设计一个支持addWord,search查询的数据结构,要求search支持.正则查询 思路:使用Trie数据结构,在匹配.时,从子结点中选取可行的一个继续匹配下一个字符,主要思路是基于递归 具 ...

  2. leetcode Add and Search Word - Data structure design

    我要在这里装个逼啦 class WordDictionary(object):def __init__(self):"""initialize your data str ...

  3. leetcode 211. Design Add and Search Words Data Structure | 211. 添加与搜索单词 - 数据结构设计(Java)

    题目 https://leetcode.com/problems/design-add-and-search-words-data-structure/ 题解 字典树 + 深度优先搜素,思路参考 le ...

  4. LeetCode 211. 添加与搜索单词 - 数据结构设计(Trie树)

    1. 题目 设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 ...

  5. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  6. LeetCode 616. 给字符串添加加粗标签(Trie树)

    文章目录 1. 题目 2. 解题 1. 题目 给一个字符串 s 和一个字符串列表 dict ,你需要将在字符串列表中出现过的 s 的子串添加加粗闭合标签 <b> 和 </b> ...

  7. LeetCode 758. 字符串中的加粗单词(Trie树)

    文章目录 1. 题目 2. 解题 1. 题目 给定一个关键词集合 words 和一个字符串 S,将所有 S 中出现的关键词加粗.所有在标签 <b> 和 </b> 中的字母都会加 ...

  8. LeetCode Two Sum III - Data structure design

    原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...

  9. 【HDU - 4217 】Data Structure? (线段树求第k小数)

    题干: Data structure is one of the basic skills for Computer Science students, which is a particular w ...

最新文章

  1. 对端边缘云网络计算模式:透明计算、移动边缘计算、雾计算和Cloudlet
  2. Thrift架构与使用方法
  3. java操作XML文件--读取内容
  4. wxWidgets:wxHtmlCellEvent类用法
  5. 文献学习(part83)--An Embedding Approach to Anomaly Detection
  6. 表格过滤器_不用记账软件也可以记录支出明细,这个在线协同表格很方便
  7. Tree的前序序列化
  8. 【转】“你不适合做程序员”
  9. spl_autoload_register函数
  10. Codeforces #345 Div.1
  11. 【语音隐写】基于matlab GUI LSB语音信号数字水印【含Matlab源码 619期】
  12. Java数据结构:数组模拟的队列(Queue)和环形队列(Circle Queue)
  13. spring Boot 设置tomcat端口号
  14. SCC(三):HEVC IBC
  15. Android动态设置view的大小及其位置
  16. 高级数据结构——AVL树
  17. URI中有关@符号的一些猥琐idea
  18. 计算机软考软件设计师2019试题,软考2019下半年软件设计师上午真题.pdf
  19. 位图文件(BMP)格式分析
  20. avahi-daemon启动失败-解决方法-linux

热门文章

  1. 科普 | Shell中傻傻分不清楚的TOP3
  2. 问懵逼:请站在 JVM 角度谈谈 Java 的锁?
  3. 我的编码习惯 —— API 接口定义
  4. Refresh your Java skills–面对Java学习过程中的一些迷茫
  5. Spring Boot自动配置原理、实战
  6. 也许,DOM 不是答案
  7. IO:Reactor和Proactor的区别
  8. Android --- 解决 registerLocationListener 过时问题(百度地图)API
  9. 人工智能导论 王万良教授_学会动态丨辽宁省人工智能导论教学研讨活动在沈阳成功举办...
  10. mqtt linux 编译,MQTT客户端代码X64位Ubuntu环境编译+测试实践小结