国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

211. Add and Search Word - Data structure design

2019-11-08 02:12:48
字體:
供稿:網(wǎng)友

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”) -> false search(“bad”) -> true search(“.ad”) -> true search(“b..”) -> true Note: You may assume that all words are consist of lowercase letters a-z.

class TrieNode { TrieNode[] children = new TrieNode[26]; boolean isWord = false; TrieNode() {}}public class WordDictionary { TrieNode root; /** Initialize your data structure here. */ public WordDictionary() { root = new TrieNode(); } /** Adds a word into the data structure. */ public void addWord(String word) { TrieNode tn = root; for (int i = 0; i < word.length(); i++) { char c = word.charAt(i); if (tn.children[c-'a'] == null) { tn.children[c-'a'] = new TrieNode(); } tn = tn.children[c-'a']; } tn.isWord = true; } /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */ public boolean search(String word) { return backtrace(word, 0, root); } public boolean backtrace(String word, int start, TrieNode root) { if (start == word.length()) return root.isWord; if (word.charAt(start) != '.') { return root.children[word.charAt(start)-'a'] != null && backtrace(word, start+1, root.children[word.charAt(start)-'a']); } else { for (int i = 0; i < 26; i++) { if (root.children[i] != null) { if (backtrace(word, start+1, root.children[i])) return true; } } } return false; }}/** * Your WordDictionary object will be instantiated and called as such: * WordDictionary obj = new WordDictionary(); * obj.addWord(word); * boolean param_2 = obj.search(word); */class TrieNode {public: TrieNode* next[26]; bool isWord; TrieNode() { memset(next, 0, sizeof(next)); isWord = false; }};class WordDictionary {public: TrieNode* root; /** Initialize your data structure here. */ WordDictionary() { root = new TrieNode(); } /** Adds a word into the data structure. */ void addWord(string word) { TrieNode* tn = root; for (int i = 0; i < word.size(); i++) { char c = word[i]; if (tn->next[c-'a'] == NULL) tn->next[c-'a'] = new TrieNode(); tn = tn->next[c-'a']; } tn->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 backtrace(word, 0, root); } bool backtrace(string word, int index, TrieNode* node) { if (index == word.size()) return node->isWord; if (word[index] != '.') { return node->next[word[index]-'a'] != NULL && backtrace(word, index+1, node->next[word[index]-'a']); } else { for (int i = 0; i < 26; i++) { if (node->next[i] != NULL) { if (backtrace(word, index+1, node->next[i])) return true; } } } return false; }};/** * Your WordDictionary object will be instantiated and called as such: * WordDictionary obj = new WordDictionary(); * obj.addWord(word); * bool param_2 = obj.search(word); */
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 二连浩特市| 微山县| 县级市| 长垣县| 平阴县| 平谷区| 宁蒗| 临桂县| 泗水县| 大姚县| 镶黄旗| 桐柏县| 合川市| 武冈市| 远安县| 临朐县| 朝阳市| 霍林郭勒市| 墨竹工卡县| 胶州市| 剑河县| 阳西县| 镶黄旗| 九台市| 栖霞市| 平泉县| 怀远县| 长宁县| 巴青县| 宜君县| 崇左市| 双桥区| 桦南县| 峡江县| 延寿县| 科技| 多伦县| 林口县| 藁城市| 平阴县| 高碑店市|