当前位置:网站首页>再战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
边栏推荐
- MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
- Chapter V project quality management of information system project manager summary
- Use of MySQL command line client and common commands
- Onenet connection process
- 微软是如何解决 PC 端程序多开问题的——内部实现
- Realize QQ login with PHP
- tf. keras. layers. Density function
- AOT和单文件发布对程序性能的影响
- Tips in MATLAB
- Xamarin效果第二十二篇之录音效果
猜你喜欢

腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
![Introduction to ACM [inclusion exclusion theorem]](/img/3a/9bc2a972d7587aab51fceb8cd2b9bd.png)
Introduction to ACM [inclusion exclusion theorem]

利用栈的回溯来解决“文件的最长绝对路径”问题
![Introduction to ACM [TSP problem]](/img/9f/4e3592542d989b2fbb6d82f7f2fbd2.png)
Introduction to ACM [TSP problem]

Distributed system services

由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。

Deep q-network (dqn)

Sonic cloud real machine tutorial

Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)

tf. keras. layers. Density function
随机推荐
Use of MySQL command line client and common commands
Guangcheng cloud service can fill in a daily report regularly every day
C# 11 对 ref 和 struct 的改进
Cherno_ Game engine series tutorial (5): 101~
Winsock programming interface experiment: Ping
Laravel new route file
[Euler plan question 13] sum of large numbers
Classification of technology selection (2022)
Source Generator实战
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
The difference between encodeuri and encodeuricomponent
Openfeign details show
腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
C#中元组对象Tuple的使用
TP5 multi conditional where query (using PHP variables)
tf. keras. layers. Inputlayer function
Numpy stack function
使用栈来解决”迷你语法分析器“的问题
C# 读写二进制文件
tf. keras. layers. Embedding function