leetcode 211. Add and Search Word
来源:互联网 发布:推荐系统 知乎 编辑:程序博客网 时间:2024/05/19 01:31
211. Add and Search Word - Data structure design
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 letters a-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 TNode{public: char key; bool isword; TNode *child[26]; TNode() { key=0; isword=false; memset(child, 0, sizeof(TNode *) * 26); } TNode(char c) { key=c; isword=false; memset(child, 0, sizeof(TNode *) * 26); }};class WordDictionary {private: TNode *root; public: /** Initialize your data structure here. */ WordDictionary() { root = new TNode(); } /** Adds a word into the data structure. */ void addWord(string word) { TNode *p = root; for (int i = 0; i < word.size(); i++) { if ( p->child[word[i]-'a'] == 0 ) { TNode *pNode = new TNode(word[i]); p->child[word[i]-'a'] = pNode; } p = p->child[word[i]-'a']; } p->isword = 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 search(root, word, 0); } bool search(TNode * pNode, string word, int pos) { TNode *p = pNode; for(int i = pos; i < word.size(); i++) { if (p && word[i] == '.') { for(int j = 0; j < 26; j++) { if (search(p->child[j], word, i+1)) return true; } return false; } else if (p && word[i] != '.') { p = p->child[word[i]-'a']; } else break; } return (p && p->isword); }};/** * Your WordDictionary object will be instantiated and called as such: * WordDictionary obj = new WordDictionary(); * obj.addWord(word); * bool param_2 = obj.search(word); */
阅读全文
0 0
- Leetcode 211. Add and Search Word
- [Leetcode] 211. Add and Search Word
- [leetcode]211. Add and Search Word
- leetcode 211. Add and Search Word
- leetcode 211. Add and Search Word
- leetcode 211. Add and Search Word
- [LeetCode] Add and Search Word
- leetcode--Add and Search Word
- 211. Add and Search Word
- 211. Add and Search Word
- 211. Add and Search Word
- [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
- Java语言程序设计(基础篇)(原书第10版) 练习题答案 第4章
- 十周 项目1(4)
- IntelliJ Idea 2017 免费激活方法
- 简单了解阿里云Web应用防火墙(上篇)
- MyApplication
- leetcode 211. Add and Search Word
- 欢迎使用CSDN-markdown编辑器
- 云星数据---Scala实战系列(精品版)】:Scala入门教程006-Scala数组详解005
- 欢迎使用CSDN-markdown编辑器(留档)
- C/C++ 第七周栈和队列 (二)队列数组 项目四
- laravel迁移相关的笔记
- 【第十二周项目1】Prim算法的验证
- AngularJs过滤器
- web 开发安全性问题