LeetCode——Add and Search Word - Data structure design
Description:
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
Note:
You may assume that all words are consist of lowercase letters a-z.
题目很好理解:添加单词查找单词。
首先想到的是用线性数据结构,然后逐个匹配查找。可以想象一定是超时的。
public class WordDictionary {
private List<String> arr = new ArrayList<String>();
// Adds a word into the data structure.
public void addWord(String word) {
arr.add(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) {
boolean flag = true;
for(String str : arr) {
if(str.equals(word)) {
flag = true;
break;
}
else if(arr.contains(".")) {
if(word.length() != str.length()) {
flag = false;
break;
}
for(int i=0; i<str.length();) {
char ch1 = str.charAt(i);
char ch2 = word.charAt(i);
if(ch1 == '.' || ch2 == '.' || ch1 == ch2) {
i ++;
}
else {
flag = false;
break;
}
}
}
else {
flag = false;
break;
}
}
return flag;
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
要优化明显要使用Trie树来减少无用的比较次数,从而降低时间复杂度。
关于Trie树:http://www.cnblogs.com/wxisme/p/4876197.html
public class WordDictionary {
private TrieNode root;
public WordDictionary() {
this.root = new TrieNode();
}
private class TrieNode {
private TrieNode[] son;
private char val;
private boolean isEnd;
public TrieNode() {
this.son = new TrieNode[26];
this.isEnd = false;
}
}
// Adds a word into the data structure.
public void addWord(String word) {
char[] wordChars = word.toCharArray();
TrieNode node = this.root;
for(char ch : wordChars) {
int pos = ch - 'a';
if(node.son[pos] == null) {
node.son[pos] = new TrieNode();
node.son[pos].val = ch;
}
node = node.son[pos];
}
node.isEnd = true;
}
public boolean patternSearch(String word, TrieNode node) {
char[] wordChars = word.toCharArray();
for(int at=0; at<word.length(); at++) {
char ch = wordChars[at];
if(ch != '.') {
int pos = ch - 'a';
if(node.son[pos] != null) {
node = node.son[pos];
}
else {
return false;
}
}
else {
int flag = 0;
for(int i=0; i<26; i++) {
if(node.son[i] != null) {
boolean b =patternSearch(word.substring(at+1), node.son[i]);
if(b) return b;
else {
flag ++;
}
}
else {
flag ++;
continue;
}
}
if(flag == 26) {
return false;
}
}
}
return node.isEnd;
}
// 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 patternSearch(word, this.root);
}
}
LeetCode——Add and Search Word - Data structure design的更多相关文章
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- leetcode Add and Search Word - Data structure design
我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...
- LeetCode Add and Search Word - Data structure design (trie树)
题意:实现添加单词和查找单词的作用,即实现字典功能. 思路:'.' 可以代表一个任何小写字母,可能是".abc"或者"a.bc"或者"abc.&quo ...
- leetcode面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...
- 字典树(查找树) 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 ...
- LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design
字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith ...
- 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(wo ...
随机推荐
- C++中的类继承之单继承&多继承&菱形继承
C++中的类继承之单继承&多继承&菱形继承 单继承是一般的单一继承,一个子类只 有一个直接父类时称这个继承关系为单继承.这种关系比较简单是一对一的关系: 多继承是指 一个子类有两个或 ...
- jquery树形表格实现方法
效果图 准备步骤: 具体使用的Dome可以在这个位置下载 http://download.csdn.net/detail/jine515073/7986227 1.引入jquery.treeTable ...
- Select显示多级分类列表
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js学习笔记30----对象
面向对象 对象:是一个整体,对外提供一些操作.JavaScript的所有数据都可以被视为对象.简单说,所谓对象,就是一种无序的数据集合,由若干个“键值对”(key-value)构成. 比如说: var ...
- webstorm软件使用记录
右边的那条线的去除:setting-editor-appearance-show right margin 勾选去掉
- 论SparkStreaming的数据可靠性和一致性
转自: http://www.csdn.net/article/2015-06-21/2825011 摘要:眼下大数据领域最热门的词汇之一便是流计算了,而其中最耀眼的无疑是来自Spark社区的Spar ...
- 《FPGA全程进阶----实战演练》第二章之系统搭建
1 系统方案 对于设计一款硬件平台,首先要确定整体框架,确定各个模块所需要的芯片以及电压分配情况.图2.6是笔者曾经设计的硬件平台系统. 图2.6系统框图 对于选定一个系统方案之后,接下来做的要先去查 ...
- 编译 boost 库(win7+boost1.60+vs2008)
参见:http://blog.csdn.net/u013074465/article/details/42532527 下载boost安装包 https://sourceforge.net/proje ...
- JavaSE(八)之Collection总结
前面几篇把集合中的知识大概都详细的说了一遍,但是我觉得还是要总结一下,这样的话,可以更好的理解集合. 一.Collection接口 首先我们要一张图来说明: Collection接口,它是集合的顶层接 ...
- u3d发布成全屏的方式
using UnityEngine; using System.Collections; public class example : MonoBehaviour { public voi ...