Skip to content

Commit

Permalink
Update 0454.四数相加II.md
Browse files Browse the repository at this point in the history
修改 Java 语言版本代码,让 HashMap 相关操作更简洁
  • Loading branch information
Winson-Huang authored Apr 18, 2023
1 parent 5b4adbd commit b7ea55c
Showing 1 changed file with 3 additions and 10 deletions.
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 b7ea55c

Please sign in to comment.