From b7ea55c93b68391ecd87c39c06e2811f0ed6b98f Mon Sep 17 00:00:00 2001 From: Winson Huang Date: Tue, 18 Apr 2023 21:07:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200454.=E5=9B=9B=E6=95=B0=E7=9B=B8?= =?UTF-8?q?=E5=8A=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改 Java 语言版本代码,让 HashMap 相关操作更简洁 --- ...33\233\346\225\260\347\233\270\345\212\240II.md" | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index 411b60e872..abfc7c238f 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -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; From 747563607292b4fc84452c4f45c2d0504531a171 Mon Sep 17 00:00:00 2001 From: Winson Huang Date: Tue, 18 Apr 2023 21:19:42 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200383.=E8=B5=8E=E9=87=91=E4=BF=A1.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为 Java 语言版本的代码添加长度判断 --- "problems/0383.\350\265\216\351\207\221\344\277\241.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index d9a184b643..a3f87d4afa 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -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];