当前位置:网站首页>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
边栏推荐
- Due to 3 ²+ four ²= five ², Therefore, we call '3,4,5' as the number of Pythagorean shares, and find the array of all Pythagorean shares within n (including n).
- MAUI初体验:爽
- FileNotFoundError: [Errno 2] No such file or directory
- 荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
- Use split to solve the "most common words" problem
- 数据挖掘系列(3)_Excel的数据挖掘插件_估计分析
- Numpy stack function
- tf. keras. layers. Inputlayer function
- Openfeign details show
- C syntax sugar empty merge operator [?] And null merge assignment operator [? =]
猜你喜欢

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

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

REINFORCE

最通俗易懂的依赖注入之生命周期

Tencent video price rise: earn more than 7.4 billion a year! Pay attention to me to receive Tencent VIP members, and the weekly card is as low as 7 yuan

MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure

Openfeign timeout setting

树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统

搭建XAMPP时mysql端口被占用

最通俗易懂的依赖注入之服务容器与作用域
随机推荐
How does Microsoft solve the problem of multiple programs on PC side -- internal implementation
TP5 email (2020-05-27)
OLED多级菜单记录
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
Middle and rear binary tree
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
Load view Caton
【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
.NET7之MiniAPI(特别篇):.NET7 Preview3
Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮
Laravel's own paging query
Q-Learning & Sarsa
数据挖掘系列(3)_Excel的数据挖掘插件_估计分析
tf. keras. layers. Timedistributed function
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
How to count the number of all files in a directory under win10 system
Detailed log display of openfeign call
AC & A2C & A3C
由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。
tf. keras. layers. MaxPooling? D function