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实现如下:

public class WordDictionary extends Trie {

	public void addWord(String word) {
super.insert(word);
} // 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) {
if (word == null || word.length() == 0)
return false;
return search(word, 0, root);
} public boolean search(String word, int depth, TrieNode node) {
if (depth == word.length() - 1) {
if (word.charAt(depth) != '.') {
if (node.son[word.charAt(depth) - 'a'] != null) {
node = node.son[word.charAt(depth) - 'a'];
return node.isEnd;
} else
return false;
}
for (int i = 0; i < 26; i++) {
if (node.son[i] != null) {
TrieNode ason = node.son[i];
if (ason.isEnd)
return true;
}
}
return false;
}
if (word.charAt(depth) != '.') {
if (node.son[word.charAt(depth) - 'a'] != null) {
node = node.son[word.charAt(depth) - 'a'];
return search(word, depth + 1, node);
} else
return false;
}
for (int i = 0; i < 26; i++) {
if (node.son[i] != null) {
TrieNode ason = node.son[i];
if (search(word, depth + 1, ason))
return true;
}
}
return false;
}
} class TrieNode {
// Initialize your data structure here.
int num;// 有多少单词通过这个节点,即节点字符出现的次数
TrieNode[] son;// 所有的儿子节点
boolean isEnd;// 是不是最后一个节点
char val;// 节点的值 TrieNode() {
this.num = 1;
this.son = new TrieNode[26];
this.isEnd = false;
}
} class Trie {
protected TrieNode root; public Trie() {
root = new TrieNode();
} public void insert(String word) {
if (word == null || word.length() == 0)
return;
TrieNode node = this.root;
char[] letters = word.toCharArray();
for (int i = 0; i < word.length(); i++) {
int pos = letters[i] - 'a';
if (node.son[pos] == null) {
node.son[pos] = new TrieNode();
node.son[pos].val = letters[i];
} else {
node.son[pos].num++;
}
node = node.son[pos];
}
node.isEnd = true;
} // Returns if the word is in the trie.
public boolean search(String word) {
if (word == null || word.length() == 0) {
return false;
}
TrieNode node = root;
char[] letters = word.toCharArray();
for (int i = 0; i < word.length(); i++) {
int pos = letters[i] - 'a';
if (node.son[pos] != null) {
node = node.son[pos];
} else {
return false;
}
}
return node.isEnd;
} // Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
if (prefix == null || prefix.length() == 0) {
return false;
}
TrieNode node = root;
char[] letters = prefix.toCharArray();
for (int i = 0; i < prefix.length(); i++) {
int pos = letters[i] - 'a';
if (node.son[pos] != null) {
node = node.son[pos];
} else {
return false;
}
}
return true;
}
} // Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// 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. vim如何在多个文件中切换

    如果我们一次打开多个文件 看一下当前目录里面的文件: wangkongming@Vostro /data/webroot/testRoot/application/modules/Admin/view ...

  2. C语言内存管理(转)

    伟大的Bill Gates 曾经失言: 640K ought to be enough for everybody — Bill Gates 1981 程序员们经常编写内存管理程序,往往提心吊胆.如果 ...

  3. Book-编程珠玑-第一章

    第一章...二〇一六年十月二十五日 22:41:45 1MB存储空间里大约可以存143,000个号码; 如果每个号码都使用32位整数来表示的话,1MB存储空间里就可以存储250,000个号码; 看得迷 ...

  4. 基础知识系列☞MSSQL→约束

    遇到一个数据库设计很渣的系统··· 本来一个约束就能解决的问题·以前建库的时候也不设计好···

  5. 增值税——基础知识

    一.增值税的概念 增值税是对从事销售货物或者提供加工.修理修配劳务以及从事进出口货物的单位和个人取得的增值额为课税对象征收的一种税. 增值额是指纳税人在生产.经营或劳务活动中所创造的新增价值,即纳税人 ...

  6. C#深入浅出 C#语法中的重中之重——委托(四)

    入行半年多了,委托干什么用的还不知道,真心说不过去了,关键对这东西有点恐惧,主要是被老师吓的,记得我C#专业课老师在讲到委托时,原话是这样的,同学们,委托这个地方是难点,暂时不讲,讲了你也不懂,等你有 ...

  7. PHP简单封装MysqlHelper类

    MysqlHelper.class.php 1: <?php 2:  3: /** 4: * Mysql数据帮助类 5: */ 6: class MysqlHelper 7: { 8: func ...

  8. PHP如何释放内存之unset销毁变量并释放内存详解

    PHP的unset()函数用来清除.销毁变量,不用的变量,我们可以用unset()将它销毁.但是某些时候,用unset()却无法达到销毁变量占用的内存!我们先看一个例子: <?php $s = ...

  9. 如何使用Android中hide的类和方法进行开发?

    1.获取Android源码并进行编译. 2.编译完毕后,取出out\target\common\obj\JAVA_LIBRARIES\framework_intermediates路径下的classe ...

  10. JAVA 如何使JScrollPane中的JTextArea自动滚动到最后一行?

    1.要使JTextArea带有滚动条,需将JTextArea对象添加到JScrollPane中. JTextArea logArea = new JTextArea(15, 35); //创建JTex ...