Java for LeetCode 211 Add and Search Word - Data structure design
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的更多相关文章
- [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 ...
- (*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 ...
- leetcode@ [211] Add and Search Word - Data structure design
https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...
- leetcode 211. Add and Search Word - Data structure design Trie树
题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...
- [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 ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- 【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 ...
- 【刷题-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 ...
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
随机推荐
- 如何理解clear的css属性?
参考文章: http://www.cnblogs.com/iyangyuan/archive/2013/03/27/2983813.html clear: 只影响使用 clear样式属性的 元素本身, ...
- hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律
http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...
- 说说移动前端中 viewport (视口)
转载网络资源的文章:来源不详~~ 移动前端中常说的 viewport (视口)就是浏览器显示页面内容的屏幕区域.其中涉及几个重要概念是 dip ( device-independent pixel 设 ...
- 设置二级域名共享一级域名Cookie和删除共享Cookie
设置共享Cookie: 二级域名要想共享一级域名的cookie,只需要设置cookie.Domain = ".一级域名.com"; 删除共享Cookie: HttpCook ...
- [C++基础]关于对象的创建及内存分配
测试: #include <stdio.h>#include <QDebug> class KPoint{public: KPoint(int x, int y){ nx = ...
- IEnumerable<> ICollection <> IList<> 区别
IEnumerable< ICollection < IList区别 public interface IEnumerable { IEnumerator GetEnumerator(); ...
- Jquery中的事件和动画
在学习Jquery中的过程中我们绝大部分都用到了事件的操作,也可以说事件是Jquery中必不可少的一部分,我们常见的一些事件有单击事件,鼠标事件,键盘事件等等.在Jquery中的学习中为了能使让页面以 ...
- Visual Studio 各种版本的快捷键总结
下列快捷组合键可在工具和文档窗口中用于进行移动.关闭或导航. 命令名 快捷键 说明 视图.全屏 SHIFT + ALT + ENTER 在打开和关闭之间切换“全屏”模式. 视图.向后定位 CTRL + ...
- 汉字转拼音(pinyin4j)
1.引入依赖 <dependency> <groupId>pinyin4j.sourceforge.net</groupId> <artifactId> ...
- 使用ASP.Net WebAPI构建REST服务(一)——简单的示例
由于给予REST的Web服务非常简单易用,它越来越成为企业后端服务集成的首选方法.本文这里介绍一下如何通过微软的Asp.Net WebAPI快速构建REST-ful 服务. 首先创建一个Asp.Net ...