设计一个支持以下两个操作的数据结构:
void addWord(word)
bool search(word)
search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。 . 意味着它可以代表任何一个字母。
例如:
addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true
注意事项:
你可以假设所有单词都是由小写字母 a-z 组成的。

详见:https://leetcode.com/problems/add-and-search-word-data-structure-design/description/

Java实现:

class TrieNode {
public TrieNode[] children;
public boolean isWord = false;
public TrieNode(){
children=new TrieNode[26];
}
} class WordDictionary {
private 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 node = root;
for (char ch: word.toCharArray()) {
if (node.children[ch-'a'] == null) {
node.children[ch-'a'] = new TrieNode();
}
node = node.children[ch-'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. */
public boolean search(String word) {
return helper(word, 0, root);
} private boolean helper(String word, int start, TrieNode node) {
if (start == word.length()){
return node.isWord;
}
char ch = word.charAt(start);
if (ch == '.') {
for (int i = 0; i < 26; i++) {
if (node.children[i] != null && helper(word, start+1, node.children[i])) {
return true;
}
}
} else {
return node.children[ch-'a'] != null && helper(word, start+1, node.children[ch-'a']);
}
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);
*/

C++实现:

class TrieNode {
public:
TrieNode *next[26];
char c;
bool isWord;
TrieNode() : isWord(false) {
for (auto & c: next)
{
c=nullptr;
}
}
TrieNode(char _c):c(_c),isWord(false)
{
for(auto &c:next)
{
c=nullptr;
}
}
};
class WordDictionary {
public:
WordDictionary() {
root = new TrieNode();
} // Adds a word into the data structure.
void addWord(string word) {
TrieNode *p = root;
for (auto &c : word)
{
int i = c - 'a';
if (!p->next[i])
{
p->next[i] = new TrieNode(c);
}
p = p->next[i];
}
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 searchWord(word, root, 0);
} bool searchWord(string &word, TrieNode *p, int i) {
if (i == word.size())
{
return p->isWord;
}
if (word[i] == '.')
{
for (auto &c : p->next)
{
if (c && searchWord(word, c, i + 1))
{
return true;
}
}
return false;
}
else
{
return p->next[word[i] - 'a'] && searchWord(word, p->next[word[i] - 'a'], i + 1);
}
} private:
TrieNode *root;
}; // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");

参考:https://www.cnblogs.com/grandyang/p/4507286.html

最新文章

  1. Dom元素的操作
  2. 在Linux上挂载Windows共享文件夹,如何开机自动挂载(mount)?
  3. PHP Log时时查看小工具
  4. 个人觉得目前 最好用的Taobao API的NodeJS封装
  5. web_reg_find()函数的使用
  6. 数往知来 三层架构 &lt;十四&gt;
  7. 为Android Studio 项目手动下载gradle
  8. 文件TEXTBOX
  9. bzoj1705
  10. firefox 自写底层扩展,源码简介
  11. 使用siege对web接口进行post方式的压力测试
  12. Android项目实战(三十五):多渠道打包
  13. SharePoint 2013 新特性 (三) 破改式 &mdash;&mdash; 设计管理器的使用 [1.设备通道]
  14. TCP/IP学习20180630-数据链路层-router choose
  15. 3.2Python数据处理篇之Numpy系列(二)--- ndarray数组的创建与变换
  16. zzzp0371 属于本人
  17. ODS设计
  18. Linux下JDK到底应该安装在哪儿?
  19. 【转】VMware网络连接模式—桥接、NAT以及仅主机模式的详细介绍和区别
  20. c#只读字段和常量的区别,以及静态构造函数的使用 .

热门文章

  1. OSChinaclient源代码学习(2)--缓存的设计
  2. 运算符与类型转换 mogondb简介
  3. 一起talk C栗子吧(第一百二十四回:C语言实例--内置宏)
  4. anaconda中新rdkit安装
  5. ubuntu下,创建ruby环境时出现 checking for Magick-config... no
  6. JAVA学习(七):方法重载与方法重写、thiskeyword和superkeyword
  7. The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF
  8. NS3网络仿真(4): DataRate属性
  9. mongodb02
  10. bzoj5333: [Sdoi2018]荣誉称号