Skip to content

Commit

Permalink
Merge pull request youngyangyang04#2037 from Winson-Huang/master
Browse files Browse the repository at this point in the history
Update 0454.四数相加II.md 0383.赎金信.md
  • Loading branch information
youngyangyang04 authored Apr 30, 2023
2 parents 45c3fd7 + 7475636 commit 66ad8c1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
4 changes: 4 additions & 0 deletions problems/0383.赎金信.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ Java:
```Java
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
// shortcut
if (ransomNote.length() > magazine.length()) {
return false;
}
// 定义一个哈希映射数组
int[] record = new int[26];

Expand Down
13 changes: 3 additions & 10 deletions problems/0454.四数相加II.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,14 @@ class Solution {
//统计两个数组中的元素之和,同时统计出现的次数,放入map
for (int i : nums1) {
for (int j : nums2) {
int tmp = map.getOrDefault(i + j, 0);
if (tmp == 0) {
map.put(i + j, 1);
} else {
map.replace(i + j, tmp + 1);
}
int sum = i + j;
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
}
//统计剩余的两个元素的和,在map中找是否存在相加为0的情况,同时记录次数
for (int i : nums3) {
for (int j : nums4) {
int tmp = map.getOrDefault(0 - i - j, 0);
if (tmp != 0) {
res += tmp;
}
res += map.getOrDefault(0 - i - j, 0);
}
}
return res;
Expand Down

0 comments on commit 66ad8c1

Please sign in to comment.