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
解题思路:

参考之前的Java for LeetCode 208 Implement Trie (Prefix Tree) 修改下即可,JAVA实现如下:

  1. public class WordDictionary extends Trie {
  2.  
  3. public void addWord(String word) {
  4. super.insert(word);
  5. }
  6.  
  7. // Returns if the word is in the data structure. A word could
  8. // contain the dot character '.' to represent any one letter.
  9. public boolean search(String word) {
  10. if (word == null || word.length() == 0)
  11. return false;
  12. return search(word, 0, root);
  13. }
  14.  
  15. public boolean search(String word, int depth, TrieNode node) {
  16. if (depth == word.length() - 1) {
  17. if (word.charAt(depth) != '.') {
  18. if (node.son[word.charAt(depth) - 'a'] != null) {
  19. node = node.son[word.charAt(depth) - 'a'];
  20. return node.isEnd;
  21. } else
  22. return false;
  23. }
  24. for (int i = 0; i < 26; i++) {
  25. if (node.son[i] != null) {
  26. TrieNode ason = node.son[i];
  27. if (ason.isEnd)
  28. return true;
  29. }
  30. }
  31. return false;
  32. }
  33. if (word.charAt(depth) != '.') {
  34. if (node.son[word.charAt(depth) - 'a'] != null) {
  35. node = node.son[word.charAt(depth) - 'a'];
  36. return search(word, depth + 1, node);
  37. } else
  38. return false;
  39. }
  40. for (int i = 0; i < 26; i++) {
  41. if (node.son[i] != null) {
  42. TrieNode ason = node.son[i];
  43. if (search(word, depth + 1, ason))
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. }
  50.  
  51. class TrieNode {
  52. // Initialize your data structure here.
  53. int num;// 有多少单词通过这个节点,即节点字符出现的次数
  54. TrieNode[] son;// 所有的儿子节点
  55. boolean isEnd;// 是不是最后一个节点
  56. char val;// 节点的值
  57.  
  58. TrieNode() {
  59. this.num = 1;
  60. this.son = new TrieNode[26];
  61. this.isEnd = false;
  62. }
  63. }
  64.  
  65. class Trie {
  66. protected TrieNode root;
  67.  
  68. public Trie() {
  69. root = new TrieNode();
  70. }
  71.  
  72. public void insert(String word) {
  73. if (word == null || word.length() == 0)
  74. return;
  75. TrieNode node = this.root;
  76. char[] letters = word.toCharArray();
  77. for (int i = 0; i < word.length(); i++) {
  78. int pos = letters[i] - 'a';
  79. if (node.son[pos] == null) {
  80. node.son[pos] = new TrieNode();
  81. node.son[pos].val = letters[i];
  82. } else {
  83. node.son[pos].num++;
  84. }
  85. node = node.son[pos];
  86. }
  87. node.isEnd = true;
  88. }
  89.  
  90. // Returns if the word is in the trie.
  91. public boolean search(String word) {
  92. if (word == null || word.length() == 0) {
  93. return false;
  94. }
  95. TrieNode node = root;
  96. char[] letters = word.toCharArray();
  97. for (int i = 0; i < word.length(); i++) {
  98. int pos = letters[i] - 'a';
  99. if (node.son[pos] != null) {
  100. node = node.son[pos];
  101. } else {
  102. return false;
  103. }
  104. }
  105. return node.isEnd;
  106. }
  107.  
  108. // Returns if there is any word in the trie
  109. // that starts with the given prefix.
  110. public boolean startsWith(String prefix) {
  111. if (prefix == null || prefix.length() == 0) {
  112. return false;
  113. }
  114. TrieNode node = root;
  115. char[] letters = prefix.toCharArray();
  116. for (int i = 0; i < prefix.length(); i++) {
  117. int pos = letters[i] - 'a';
  118. if (node.son[pos] != null) {
  119. node = node.son[pos];
  120. } else {
  121. return false;
  122. }
  123. }
  124. return true;
  125. }
  126. }
  127.  
  128. // Your Trie object will be instantiated and called as such:
  129. // Trie trie = new Trie();
  130. // trie.insert("somestring");
  131. // trie.search("key");

Java for LeetCode 211 Add and Search Word - Data structure design的更多相关文章

  1. [LeetCode] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  2. (*medium)LeetCode 211.Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  3. leetcode@ [211] Add and Search Word - Data structure design

    https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...

  4. leetcode 211. Add and Search Word - Data structure design Trie树

    题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...

  5. [leetcode]211. Add and Search Word - Data structure design添加查找单词 - 数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  6. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  7. 【LeetCode】211. Add and Search Word - Data structure design

    Add and Search Word - Data structure design Design a data structure that supports the following two ...

  8. 【刷题-LeetCode】211. Add and Search Word - Data structure design

    Add and Search Word - Data structure design Design a data structure that supports the following two ...

  9. 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...

随机推荐

  1. 如何理解clear的css属性?

    参考文章: http://www.cnblogs.com/iyangyuan/archive/2013/03/27/2983813.html clear: 只影响使用 clear样式属性的 元素本身, ...

  2. hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律

    http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...

  3. 说说移动前端中 viewport (视口)

    转载网络资源的文章:来源不详~~ 移动前端中常说的 viewport (视口)就是浏览器显示页面内容的屏幕区域.其中涉及几个重要概念是 dip ( device-independent pixel 设 ...

  4. 设置二级域名共享一级域名Cookie和删除共享Cookie

     设置共享Cookie: 二级域名要想共享一级域名的cookie,只需要设置cookie.Domain = ".一级域名.com";   删除共享Cookie:  HttpCook ...

  5. [C++基础]关于对象的创建及内存分配

    测试: #include <stdio.h>#include <QDebug> class KPoint{public: KPoint(int x, int y){ nx = ...

  6. IEnumerable<> ICollection <> IList<> 区别

    IEnumerable< ICollection < IList区别 public interface IEnumerable { IEnumerator GetEnumerator(); ...

  7. Jquery中的事件和动画

    在学习Jquery中的过程中我们绝大部分都用到了事件的操作,也可以说事件是Jquery中必不可少的一部分,我们常见的一些事件有单击事件,鼠标事件,键盘事件等等.在Jquery中的学习中为了能使让页面以 ...

  8. Visual Studio 各种版本的快捷键总结

    下列快捷组合键可在工具和文档窗口中用于进行移动.关闭或导航. 命令名 快捷键 说明 视图.全屏 SHIFT + ALT + ENTER 在打开和关闭之间切换“全屏”模式. 视图.向后定位 CTRL + ...

  9. 汉字转拼音(pinyin4j)

    1.引入依赖 <dependency> <groupId>pinyin4j.sourceforge.net</groupId> <artifactId> ...

  10. 使用ASP.Net WebAPI构建REST服务(一)——简单的示例

    由于给予REST的Web服务非常简单易用,它越来越成为企业后端服务集成的首选方法.本文这里介绍一下如何通过微软的Asp.Net WebAPI快速构建REST-ful 服务. 首先创建一个Asp.Net ...