当前位置:网站首页>再战leetcode (290.单词规律)
再战leetcode (290.单词规律)
2022-04-23 03:05:00 【学逗b】
290单词规律
题目描述

题解
我的题解一开始很多东西都没有考虑到,就遇到一些特殊的示例。
特殊示例
示例一
输入: pattern = “abba”, str = “dog dog dog dog”
输出: true
我一开始的代码是根据有没有再出现hashmap的key来判断,并没用判断这个value,所以这种情况是无法识别的。
示例二
输入 pattern = “aaa“ str = “dog dog dog dog”
输出: false
我一开始也老实忘了判断这些基本情况导致长度不想等也忘了考虑进去
代码
class Solution {
public boolean wordPattern(String pattern, String s) {
Map<Character, String> map = new HashMap<>();
String[] arr = s.split(" ");
int n = pattern.length();
if (n != arr.length) {
return false;
}
for (int i = 0; i < n; i++) {
char ch = pattern.charAt(i);
if (map.containsKey(ch)) {
if (!Objects.equals(map.get(ch), arr[i])) {
return false;
}
} else {
if (map.containsValue(arr[i])) {
return false;
} else {
map.put(ch, arr[i]);
}
}
}
return true;
}
}
自己写的代码跟leetcode大佬写的还是差很多,下面是leetcode大佬的代码写的代码
class Solution {
public boolean wordPattern(String pattern, String str) {
String[] words = str.split(" ");
//字符和单词是互相映射,数量必须相等
if (words.length != pattern.length()) {
return false;
}
Map<Object, Integer> map = new HashMap<>();
for (Integer i = 0; i < words.length; i++) {
/* 如果key不存在,插入成功,返回null;如果key存在,返回之前对应的value。 以pattern = "abba", str = "dog cat cat dog"为例, 第1次:map.put('a',0)返回null,map.put("dog",0)返回null,两者相等; 第2次:map.put('b',1)返回null,map.put("cat",1)返回null,两者相等; 第3次:map.put('b',2)返回1,map.put("cat",2)返回1,两者相等; 第4次:map.put('a',3)返回0,map.put("dog",3)返回0,两者相等, 结果为 true。 以pattern = "abba", str = "dog cat cat fish"为例, 第1次:map.put('a',0)返回null,map.put("dog",0)返回null,两者相等; 第2次:map.put('b',1)返回null,map.put("cat",1)返回null,两者相等; 第3次:map.put('b',2)返回1,map.put("cat",2)返回1,两者相等; 第4次:map.put('a',3)返回0,map.put("fish",3)返回null,两者不相等, 结果为 false。 */
if (map.put(pattern.charAt(i), i) != map.put(words[i], i)) {
return false;
}
}
return true;
}
}
上面为什么使用Integer 而不是用int呢?
如果是 int的话,因为 如果int的值超过 [-128,127]的话,那么这个就会重新创建一个新的Integer对象。
所以这里直接用Integer。
版权声明
本文为[学逗b]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Leiyi_Ann/article/details/124348942
边栏推荐
- Small companies don't make formal offers
- Tips in MATLAB
- Restart redis
- [software testing] understand the basic knowledge of software testing
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)
- .Net Core 限流控制-AspNetCoreRateLimit
- 微软是如何解决 PC 端程序多开问题的
- FileNotFoundError: [Errno 2] No such file or directory
- Introduction and use of openfeign component
- 使用split来解决“最常见的单词”问题
猜你喜欢

It turns out that PID was born in the struggle between Lao wangtou and Lao sky
![Niuke white moon race 6 [solution]](/img/c5/6c59378c3bb12efa60ab3a8cd2c943.png)
Niuke white moon race 6 [solution]

AspNetCore配置多环境log4net配置文件

ASP.NET 6 中间件系列 - 条件中间件

全网讲的最细,软件测试度量,怎样优化软件测试成本提高效率---火爆

Onenet connection process

Depth deterministic strategy gradient (ddpg)

Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮

Cherno_ Game engine series tutorial (5): 101~

微软是如何解决 PC 端程序多开问题的——内部实现
随机推荐
ASP.NET 6 中间件系列 - 自定义中间件类
荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
7-11 重排链表 (25 分)
HLS / chisel uses CORDIC hyperbolic system to realize square root calculation
JSON data text
Opencv fills the rectangle with a transparent color
【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
Restart redis
Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s
REINFORCE
Recursion - outputs continuously increasing numbers
FileNotFoundError: [Errno 2] No such file or directory
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
最通俗易懂的依赖注入之服务容器与作用域
[software testing] understand the basic knowledge of software testing
Traversée de l'arbre L2 - 006
[learn junit5 from official documents] [II] [writingtests] [learning notes]
最通俗易懂的依赖注入与控制反转
TP5 inherits base and uses the variables in base
Winsock programming interface experiment: implementation of ipconfig