leetcode.211. Add and Search Word - Data structure design字典树
来源:互联网 发布:剑三萝莉脸型数据网盘 编辑:程序博客网 时间:2024/05/17 22:41
Design a data structure that supports the following two operations:
void addWord(word)bool search(word)
search(word) can search a literal word or a regular expression string containing only lettersa-z
or .
. A .
means it can represent any one letter.
For example:
addWord("bad")addWord("dad")addWord("mad")search("pad") -> falsesearch("bad") -> truesearch(".ad") -> truesearch("b..") -> true
Note:
You may assume that all words are consist of lowercase letters a-z
.
用到字典树
class TrieNode { public: bool isEnd; TrieNode *children[26]; TrieNode() : isEnd(false) { for (int i = 0; i < 26; i++) { children[i] = NULL; } } }; class WordDictionary {public: WordDictionary() { root = new TrieNode(); } // Adds a word into the data structure. void addWord(string word) { TrieNode *cur = root; for (int i = 0; i < word.length(); i++) { int index = word[i] - 'a'; if (cur->children[index] == NULL) { cur->children[index] = new TrieNode(); } cur = cur->children[index]; } cur->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) { int n = word.length(); return search(word, n, 0, root); } bool search(string &word, int n, int pos, TrieNode *cur) { if (cur == NULL) { return false; } if (pos == n) { return cur->isEnd; } if (word[pos] == '.') { for (int i = 0; i < 26; i++) { if (cur->children[i]) { if (search(word, n, pos+1, cur->children[i])) { return true; } } } } else { int index = word[pos] - 'a'; if (cur->children[index]) { return search(word, n, pos+1, cur->children[index]); } } return false; } private: TrieNode *root; };
0 0
- LeetCode *** 211. Add and Search Word - Data structure design(字典树)
- leetcode.211. Add and Search Word - Data structure design字典树
- leetcode Add and Search Word - Data structure design回溯算法+Trie字典树
- [leetcode] 211.Add and Search Word - Data structure design
- LeetCode 211. Add and Search Word - Data structure design
- [Leetcode]211. Add and Search Word - Data structure design @python
- Leetcode 211. Add and Search Word - Data structure design
- 211. Add and Search Word - Data structure design LeetCode
- leetcode 211. Add and Search Word - Data structure design
- LeetCode 211. Add and Search Word - Data structure design
- Leetcode 211. Add and Search Word - Data structure design
- Leetcode 211. Add and Search Word - Data structure design
- LeetCode-211.Add and Search Word - Data structure design
- Leetcode 211. Add and Search Word - Data structure design
- LeetCode-Add and Search Word - Data structure design-前缀树
- leetcode---Add and Search Word - Data structure design---Trie树
- LeetCode 211.Add and Search Word - Data structure design(trie树)
- 211.leetcode Add and Search Word - Data structure design(medium)[Trie树]
- 混淆apk:Android中使用eclipse混淆apk和studio混淆app的不同
- 以太网技术原理
- Python实现对一个元组分别进行按姓名排序与按分数排序
- 译见|深度剖析「微服务架构」的九大特征
- HDU2184 汉诺塔8
- leetcode.211. Add and Search Word - Data structure design字典树
- Android Audio回声消除学习笔记
- 流式统计的几个难点
- 亿级用户下的新浪微博平台架构
- IOS跳转到应用下面的的设置
- jquery获取和改变值
- 129. Sum Root to Leaf Numbers
- 阿里云部署 Flask + WSGI + Nginx 详解
- python读取文件名后重命名原文件