Java实现 LeetCode 211 添加与搜索单词 - 数据结构设计
211. 添加与搜索单词 - 数据结构设计
设计一个支持以下两种操作的数据结构:
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 组成的。
class WordDictionary {
Map<Integer,Set<String>> map = new HashMap<>();//根据字符串长度分开存放
public WordDictionary() {
}
public void addWord(String word) {
int length = word.length();
if(map.get(length)!=null){
map.get(length).add(word);
}else{
Set<String> set = new HashSet<>();
set.add(word);
map.put(length, set);
}
}
public boolean search(String word) {
Set<String> set = map.get(word.length());
if(set==null){ //不存在该长度的字符串,直接返回false;
return false;
}
if(set.contains(word)) return true;
char[] chars = word.toCharArray();
P:for(String s : set){
if(word.length()!=s.length()){
continue;
}
char[] cs = s.toCharArray();
for(int i = 0; i< cs.length; i++){//逐个字符对比
if(chars[i] != '.' && chars[i] != cs[i]){
continue P;
}
}
set.add(word);
return true;
}
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);
*/
Java实现 LeetCode 211 添加与搜索单词 - 数据结构设计的更多相关文章
- leetcode 211. 添加与搜索单词 - 数据结构设计 解题报告
设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...
- [LeetCode] 211. 添加与搜索单词 - 数据结构设计
题目链接:https://leetcode-cn.com/problems/add-and-search-word-data-structure-design/ 题目描述: 设计一个支持以下两种操作的 ...
- Leetcode 211.添加与搜索单词
添加与搜索单词 设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字 ...
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
- 211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两个操作的数据结构:void addWord(word)bool search(word)search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . ...
- [Swift]LeetCode211. 添加与搜索单词 - 数据结构设计 | Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- Leetcode211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...
- [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] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
随机推荐
- quartus ii FFT核使用
quartus ii FFT核使用 导入自己程序自带的txt文件,写出控制模块 时序图 FFT核文件给出的时序图输入 仿真时序图 1024个采样点数,输入结束 fft数据输出 2.代码 `timesc ...
- JS字符串截取 “指定字符” 前面和后面的内容!
JS字符串截取 “指定字符” 前面和后面的内容! var string= "07/12" var before = string.split('/')[0] var after = ...
- 使用Optional,不再头疼NPE
前言 在 Java 语言开发中,可能大多数程序员遇到最多的异常就是 NullPointException 空指针异常了.这个当初语言的开发者"仅仅因为这样实现起来更容易"而允许空引 ...
- [hdu2594]kmp水题
题意:求最长的a的前缀同时满足是b的后缀,把a,b连在一起,kmp跑一下,迭代next直到长度小于等于a,b长度的最小值为止,即为答案. #pragma comment(linker, "/ ...
- 用项目强化你的webpack
用你的webpack实现vue-cli 本文围绕前端工程化,用webpack从零搭建一个完整项目的过程 本文核心知识点: webpack的使用 vue组件化思想 Element-UI的使用 别走别走, ...
- 有一分数序列:2/1, 3/2, 5/3, 8/5, 13/8, 21/13....求出这个数列的第M到N项之和(M>2,N>2,N>M)
package bianchengti; /* * 有一分数序列:2/1, 3/2, 5/3, 8/5, 13/8, 21/13.... * 求出这个数列的第M到N项之和(M>2,N>2, ...
- form组件注册ajax登录auth认证及验证码
本项目采用django自带的数据库 项目文件 models.py from django.db import models from django.contrib.auth.models import ...
- html页面加载顺序
页面总是从上往下执行 CSS为什么要放在头部 1.CSS可以和html一起同时进行解析和渲染 2.如果你把CSS放到body后面,不但没有跟html一起进行加载渲染,还要花费额外时间去加载CSS,这样 ...
- IDEA三种注释详解
三种注释方式 行注释.块注释.方法或类说明注释. 一.快捷键:Ctrl + / 使用Ctrl+ /, 添加行注释,再次使用,去掉行注释 二.演示代码 if (hallSites != null &am ...
- PAT-1060 Are They Equal (科学计数法)
1060. Are They Equal If a machine can save only 3 significant digits, the float numbers 12300 and 1 ...