前缀树解法:

struct TrieNode
{bool isEnd;TrieNode *next[26];TrieNode(){isEnd = false;memset(next, NULL, sizeof(next));}
};
class WordDictionary {private:TrieNode *root;bool searchUtil(string word, int start, TrieNode *root){TrieNode *node = root;for(int i = start; i < word.size(); i++){char ch = word[i];if(ch == '.'){for(int j = 0; j < 26; j++){//将第i个字符分别改成a~z去递归string temp = word;temp[i] = 'a' + j;if(searchUtil(temp, i, node) == true){//匹配到一个符合的单词return true; }}//把.换成a~z都不行return false;}else if(node->next[ch - 'a'] == NULL){return false;}else{node = node->next[ch - 'a'];}}return node->isEnd;}
public:/** Initialize your data structure here. */WordDictionary() {root =  new TrieNode();}/** Adds a word into the data structure. */void addWord(string word) {TrieNode *node = root;for(int i = 0; i < word.size(); i++){char ch = word[i];if(node->next[ch - 'a'] == NULL){node->next[ch - 'a'] = new TrieNode();            }node = node->next[ch - 'a'];}node->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) {return searchUtil(word, 0 ,root);}
};/*** Your WordDictionary object will be instantiated and called as such:* WordDictionary* obj = new WordDictionary();* obj->addWord(word);* bool param_2 = obj->search(word);*/

[leetcode]211. 添加与搜索单词 - 数据结构设计 ---前缀树解法相关推荐

  1. LeetCode—211. 添加与搜索单词 - 数据结构设计

    211. 添加与搜索单词 - 数据结构设计 题目描述:请你设计一个数据结构,支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 . 实现词典类 WordDictionary : WordD ...

  2. Java实现 LeetCode 211 添加与搜索单词 - 数据结构设计

    211. 添加与搜索单词 - 数据结构设计 设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则 ...

  3. Leetcode 211. 添加与搜索单词 - 数据结构设计 解题思路及C++实现

    解题思路: 用字典树来作为存储的数据结构. 新增单词的时候,就使用字典树插入新单词的方法,与LeetCode 208 题一样. 在查找某一个字典树时,使用深度优先搜索即可. class WordDic ...

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

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

  5. 211. 添加与搜索单词 - 数据结构设计

    211. 添加与搜索单词 - 数据结构设计 请你设计一个数据结构,支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 . 实现词典类 WordDictionary : WordDictio ...

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

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

  7. 2022-9-18 Leetcode211.211. 添加与搜索单词 - 数据结构设计【前缀树】

    使用vector比较好,使用map会造成超时. struct Node {bool isWord;vector<Node*> next;Node():isWord(false) {next ...

  8. 【LeetCode】【HOT】208. 实现 Trie (前缀树)

    [LeetCode][HOT]208. 实现 Trie (前缀树) 文章目录 [LeetCode][HOT]208. 实现 Trie (前缀树) package hot;public class So ...

  9. LeetCode 170. 两数之和 III - 数据结构设计(哈希map)

    文章目录 1. 题目 2. 解题 1. 题目 设计并实现一个 TwoSum 的类,使该类需要支持 add 和 find 的操作. add 操作 - 对内部数据结构增加一个数. find 操作 - 寻找 ...

最新文章

  1. vue中显示和隐藏如何做动画_vue-State Transitions(状态转换)
  2. goland 远程调试 golang
  3. 大数据之mongodb -- (2)java集成 MongoDB 3.2,使用Spring-data-mongodb进行集成
  4. 每日英语:Vender Assault Shines Ugly Light on China's Urban Enforcers
  5. 如何避免form提交进行页面跳转
  6. 高效精准分析定位系统BUG,让你无所不能
  7. 笛卡尔函数_笛卡尔,伯克利和函数式React式编程
  8. python赋值运算符_解释一下python中的赋值运算符
  9. unity2019汉化
  10. 计算机硬件开票几个点,财务税控开票电脑装机配置清单和价格介绍
  11. 安装win10虚拟机遇到的坑
  12. 《漫步华尔街》书中的精髓:在美国市场中,怎样用“随机漫步”的投资方法让自己的投资收益稳步增长
  13. 用python画圆角矩形_如何用OpenCV绘制圆角矩形(带圆角的矩形)?
  14. 2019上海市大学生网络安全大赛部分web题解
  15. Landesk学习笔记1_Landesk三种拖送方式
  16. Go设计模式--访客模式
  17. 管理学十二(流程与制度的重要性)
  18. leetcode: 70. Climbing Stairs
  19. mAPI(商户用API) v1.1发布
  20. EffectiveC++详解:条款05-了解C++默默编写并调用哪些函数

热门文章

  1. python骗局-代购被骗?Python带你征服骗子!直接干掉他的网站!亲身经历!
  2. 手机上有没有学python的软件-盘点几个在手机上可以用来学习编程的软件
  3. 免费python课程排行榜-Python基础练习(一)中国大学定向排名爬取
  4. python能做什么游戏-Python有做大型游戏的潜力吗?
  5. 语音识别电路设计图集锦 - 嵌入式类电子电路图 - 电子发烧友网
  6. 讯飞庭审语音识别系统亮相最高人民法院工作报告
  7. 汉语语音情绪识别,Emotion Recognition by Speech Signal in Mandarin,音标,读音,翻译,英文例句,英语词典...
  8. 【java笔记】异常处理
  9. 最简单的基于FFMPEG的音频编码器(PCM编码为AAC)
  10. LIRE的使用:搜索相似的图片 1