lintcode-473-单词的添加与查找
2017-08-21 12:41
507 查看
473-单词的添加与查找
设计一个包含下面两个操作的数据结构:addWord(word), search(word)
addWord(word)会在数据结构中添加一个单词。而search(word)则支持普通的单词查询或是只包含.和a-z的简易正则表达式的查询。
一个 . 可以代表一个任何的字母。
注意事项
你可以假设所有的单词都只包含小写字母 a-z。样例
addWord("bad")addWord("dad")
addWord("mad")
search("pad") // return false
search("bad") // return true
search(".ad") // return true
search("b..") // return true
标签
字典树思路
使用字典树,如何实现一个字典树相关见 lintcode-442-实现 TrieaddWord 对应字典树的插入操作
search 对应字典树查询操作,不过需要注意 '.' (正则表达式)的操作,因为 '.' 可以对应任何字符,所以要遍历 '.' 对应节点的所有子树,而非像其他字符那样,遍历对应的一条子树就可以。且所有子树有一条符合既可以证明存在此单词,所以要将遍历结果执行 '或' 操作
code
/** * Your Trie object will be instantiated and called as such: * Trie trie; * trie.insert("lintcode"); * trie.search("lint"); will return false * trie.startsWith("lint"); will return true */ class TrieNode { public: // Initialize your data structure here. char c; TrieNode * next[26]; bool isEnd; TrieNode() { c = ' '; for (int i = 0; i < 26; i++) { next[i] = nullptr; } isEnd = false; } TrieNode(char c) { this->c = c; for (int i = 0; i < 26; i++) { next[i] = nullptr; } isEnd = false; } }; class WordDictionary { private: TrieNode* root; public: WordDictionary() { root = new TrieNode(); } // Adds a word into the data structure. void addWord(string word) { // Write your code here TrieNode* curNode = root; for (int i = 0; i < word.size(); i++) { if (curNode->next[word[i] - 'a'] != nullptr) { curNode = curNode->next[word[i] - 'a']; } else { TrieNode* node = new TrieNode(word[i]); curNode->next[word[i] - 'a'] = node; curNode = node; } } curNode->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) { // Write your code here return search(word, root); } bool search(string word, TrieNode* root) { if (word.size() == 0 && root != NULL) { return root->isEnd; } else if (word[0] != '.' && root != NULL) { return search(word.substr(1, word.size() - 1), root->next[word[0] - 'a']); } else if (word[0] == '.' && root != NULL) { bool result = false; for (int i = 0; i < 26; i++) { if (root->next[i] != nullptr) { result = result | search(word.substr(1, word.size() - 1), root->next[i]); } } return result; } else if (root == NULL) { return false; } } }; // Your WordDictionary object will be instantiated and called as such: // WordDictionary wordDictionary; // wordDictionary.addWord("word"); // wordDictionary.search("pattern");
相关文章推荐
- 473 - 单词的添加与查找
- [LintCode] Add and Search Word 添加和查找单词
- 单词的添加与查找-LintCode
- Lintcode 单词的添加与查找
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
- [LeetCode] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计
- Lintcode---单词的添加与查找
- 构造一个单词查找树,支持以下两个操作的数据结构: 添加和搜索,只包含字母a-z或.。
- [ACM] POJ 1035 Spell checker (单词查找,删除替换添加不论什么一个字母)
- lintcode---实现trie--单词的添加与查找
- 20:查找兄弟单词
- Word中,英文单词在换行时,如何自动添加一个"-"?(转)
- lintcode 容易题:strStr 字符串查找
- 【vi】awk为指定行的指定字段添加一个单词
- 最近发现系统rabbitmq丢消息比较严重,于是想了些方案来查找原因,给将消息发送方式添加确认机制。 我们在本地模拟了wms发送打标消息的场景. 1. 有事务 2. 先发点对点队列, 再发订
- 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "StdAfx.h"”?
- 字符串查找----三向单词查找树
- eclipse 快捷键大全,eclipse查找类,文件,添加注释
- fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h
- fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h