当前位置:网站首页>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
边栏推荐
- Passing object type parameters through openfeign
- TP5 multi conditional where query (using PHP variables)
- Using stack to solve the problem of "mini parser"
- 使用split来解决“最常见的单词”问题
- Deep q-network (dqn)
- 使用DFS来解决“字典序排数”问题
- 全网最全,接口自动化测试怎么做的?精通接口自动化测试详解
- 数据挖掘系列(3)_Excel的数据挖掘插件_估计分析
- 使用两种方法来解决“最大回文数乘积”问题
- TP5 inherits base and uses the variables in base
猜你喜欢

Detailed log display of openfeign call

編碼電機PID調試(速度環|比特置環|跟隨)

MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator

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

数据挖掘系列(3)_Excel的数据挖掘插件_估计分析

The backtracking of stack is used to solve the problem of "the longest absolute path of file"

AC & A2C & A3C

. net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure

tf. keras. layers. Conv? D function

Maui initial experience: Cool
随机推荐
ASP.NET 6 中间件系列 - 自定义中间件类
Dynamic sequence table + OJ
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
OLED多级菜单记录
最通俗易懂的依赖注入之服务容器与作用域
MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator
Introduction and use of openfeign component
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)
Response processing of openfeign
The most detailed in the whole network, software testing measurement, how to optimize software testing cost and improve efficiency --- hot
Xamarin effect Chapter 21 expandable floating operation button in GIS
Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮
宁德时代地位不保?
建立与遍历二叉树
tf. keras. layers. Timedistributed function
搭建XAMPP时mysql端口被占用
ASP. Net 6 middleware series - Custom middleware classes
ASP.NET 6 中间件系列 - 执行顺序
Laravel's own paging query
SQL statement - DDL