[LeetCode] Add and Search Word - Data structure design
来源:互联网 发布:伦纳德数据 编辑:程序博客网 时间:2024/04/30 01:17
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
.
解题思路:
运用前缀树来实现词典的查询。唯一不同的是有通配符.,对于通配符,运用暴力回溯搜索法即可。
struct dictionaryNode{public: dictionaryNode* sons[26]; bool isWord; dictionaryNode(){ for(int i=0; i<26; i++){ sons[i]=NULL; } isWord=false; } ~dictionaryNode(){ for(int i=0; i<26; i++){ if(sons[i]!=NULL){ delete sons[i]; sons[i]=NULL; } } }};class WordDictionary {public: dictionaryNode* root; WordDictionary(){ root=new dictionaryNode(); } ~WordDictionary(){ if(root!=NULL){ delete root; root = NULL; } } // Adds a word into the data structure. void addWord(string word) { dictionaryNode* node = root; int len=word.length(); for(int i=0; i<len; i++){ if(node->sons[word[i]-'a']==NULL){ node->sons[word[i]-'a'] = new dictionaryNode(); } node = node->sons[word[i]-'a']; } node->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 searchHelp(root, word, 0); } bool searchHelp(dictionaryNode* node, string& word, int i){ if(node==NULL){ return false; } int len = word.length(); if(i>=len){ return node->isWord; } if(word[i]!='.'){ return searchHelp(node->sons[word[i]-'a'], word, i+1); }else{ for(int k=0; k<26; k++){ if(searchHelp(node->sons[k], word, i+1)){ return true; } } return false; } }};// Your WordDictionary object will be instantiated and called as such:// WordDictionary wordDictionary;// wordDictionary.addWord("word");// wordDictionary.search("pattern");
0 0
- 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
- leetcode:Add and Search Word - Data structure design
- [leetcode]Add and Search Word - Data structure design
- [LeetCode] 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,解题报告
- 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
- Leetcode: Add and Search Word - Data structure design
- [leetcode] Add and Search Word - Data structure design
- Leetcode: Add and Search Word - Data structure design (Java)
- leetcode Add and Search Word - Data structure design
- [leetcode] Add and Search Word - Data structure design
- leetcode--Add and Search Word - Data structure design
- 深入理解事务--事务ACID特性及隔离级别
- 个人PHP学习笔记(二)--语句结构
- Rcurl抓取NCBI信息
- 使用Java实现双缓冲绘图
- Remove Linked List Elements
- [LeetCode] Add and Search Word - Data structure design
- LeetCode解题报告--Longest Palindromic Substring
- 1005. 7.2 Printing distinct numbers
- Android 判断手机是否联网时异常闪退
- 威威猫系列故事——打地鼠(HDU-4540)
- Triangle -- leetcode
- Cocosd-x(3.4版本)移植Android平台
- java的反射类讲解
- Android笔记2——Android的用户界面1