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 ...
随机推荐
- Viterbi-Algorithm(维特比)算法
CSDN博客:皮乾东 知乎:Htrying 微博:Htring的微博 微信公众号:自然语言处理爱好者(ID:NLP_lover) 文章来自:<数学之美> Viterbi-Algor ...
- Properties集合与IO流
package com.itheima.demo07.Prop; import java.io.FileOutputStream; import java.io.FileReader; import ...
- Python自动生成100以内加减乘除混合运算题
import random from random import choice ops = ('+','-','×','÷') ans = [] i=0 while i < 100 : op1 ...
- 网鼎杯2020青龙组writeup-web
本文首发于Leon的Blog,如需转载请注明原创地址并联系作者 AreUSerialz 开题即送源码: <?php include("flag.php"); highligh ...
- tcp/ip 学习(一)
TCP/IP协议是什么? TCP:Transmission Control Protocol 传输控制协议 IP:Internet Protocol 因特网协议 简单来说,TCP/IP协议就是一个 ...
- Java集合类: Set、List、Map
Set.List.Map都是集合接口 set --其中的值不允许重复,无序的数据结构 list --其中的值允许重复,因为其为有序的数据结构 map--成对的数据结构,健值必须具有唯一 ...
- element-ui的el-table和el-form嵌套使用表单校验
表格中嵌套使用表单验证 表格是el-table自动获取的后台数据,每行都有el-input的验证,这样一个rules的规则就不能匹配到每一行,所以要是用动态的prop和rules规则 需求如下,要对告 ...
- WARN: Establishing SSL connection without server’s identity verification is not recommended
问题 使用Spring JDBC 连接 MySQL时,出现如下警告: WARN: Establishing SSL connection without server's identity verif ...
- 为什么说OC是运行时语言?什么是动态类型、动态绑定、动态加载?
转载:https://www.cnblogs.com/dxb123456/p/5525343.html 动态: 主要是将数据类型的确定由编译时,推迟到了运行时. 这个问题其实浅涉及到两个概念,运行时和 ...
- Hyperledger Fabric开发(一):环境配置
macOS系统下配置hyperledger fabric环境 cURL mac中自带curl,如果需要下载最新版本,则可按照以下命令,并将路径添加在系统环境变量的最前面即可 brew install ...