当前位置:网站首页>Fight leetcode again (290. Word law)
Fight leetcode again (290. Word law)
2022-04-23 03:07:00 【Learn to tease B】
290 Rules of words
Title Description
Answer key
At the beginning of my solution, many things were not taken into account , There are some special examples .
Special examples
Example 1
Input : pattern = “abba”, str = “dog dog dog dog”
Output : true
My initial code is based on whether it appears again hashmap Of key To judge , It's no use judging this value, So this situation is unrecognizable .
Example 2
Input pattern = “aaa“ str = “dog dog dog dog”
Output : false
At the beginning, I honestly forgot to judge these basic conditions, resulting in the length. I didn't want to wait and forgot to take it into account
Code
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;
}
}
Write your own code with leetcode What the boss wrote is still a lot worse , Here is leetcode The code written by the boss
class Solution {
public boolean wordPattern(String pattern, String str) {
String[] words = str.split(" ");
// Words and characters map to each other , The quantity must be equal
if (words.length != pattern.length()) {
return false;
}
Map<Object, Integer> map = new HashMap<>();
for (Integer i = 0; i < words.length; i++) {
/* If key non-existent , Insert the success , return null; If key There is , Returns the previous corresponding value. With pattern = "abba", str = "dog cat cat dog" For example , The first 1 Time :map.put('a',0) return null,map.put("dog",0) return null, Both equal ; The first 2 Time :map.put('b',1) return null,map.put("cat",1) return null, Both equal ; The first 3 Time :map.put('b',2) return 1,map.put("cat",2) return 1, Both equal ; The first 4 Time :map.put('a',3) return 0,map.put("dog",3) return 0, Both equal , The result is true. With pattern = "abba", str = "dog cat cat fish" For example , The first 1 Time :map.put('a',0) return null,map.put("dog",0) return null, Both equal ; The first 2 Time :map.put('b',1) return null,map.put("cat",1) return null, Both equal ; The first 3 Time :map.put('b',2) return 1,map.put("cat",2) return 1, Both equal ; The first 4 Time :map.put('a',3) return 0,map.put("fish",3) return null, The two are not equal , The result is false. */
if (map.put(pattern.charAt(i), i) != map.put(words[i], i)) {
return false;
}
}
return true;
}
}
Why does it use Integer
Rather than using int
Well ?
If it is int
Words , because If int The value of exceeds [-128,127] Words , Then this will recreate a new Integer
object .
So it's directly used here Integer.
版权声明
本文为[Learn to tease B]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230305117067.html
边栏推荐
- TP5 inherits base and uses the variables in base
- [software testing] understand the basic knowledge of software testing
- C# 11 的这个新特性,我愿称之最强!
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (9)
- Niuke white moon race 6 [solution]
- The most easy to understand dependency injection and control inversion
- 使用DFS来解决“字典序排数”问题
- 編碼電機PID調試(速度環|比特置環|跟隨)
- 樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統
- MAUI初体验:爽
猜你喜欢
FileNotFoundError: [Errno 2] No such file or directory
最通俗易懂的依赖注入之服务容器与作用域
腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
Vs code setting line feed
Recursion - outputs continuously increasing numbers
Blazor University (12)组件 — 组件生命周期
編碼電機PID調試(速度環|比特置環|跟隨)
基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
ASP. Net and ASP NETCORE multi environment configuration comparison
C# WPF UI框架MahApps切换主题
随机推荐
be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
SQL statement - DDL
使用split来解决“最常见的单词”问题
基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目
Blazor University (11)组件 — 替换子组件的属性
Realize QQ login with PHP
tf. keras. layers. Conv? D function
TP5 customization in extend directory succeeded and failed. Return information
Systemctl start Prometheus + grafana environment
Maui initial experience: Cool
编码电机PID调试(速度环|位置环|跟随)
C# 读写二进制文件
Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)
Recursion - outputs continuously increasing numbers
.Net Core 限流控制-AspNetCoreRateLimit
Use of slice grammar sugar in C #
[software testing] understand the basic knowledge of software testing
MYSQL_ From mastery to abandonment
Use split to solve the "most common words" problem
Response processing of openfeign