From d01fd238549bc10a6bd5b883f91d60d1ed2ab419 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Fri, 3 Sep 2021 13:23:03 +0800 Subject: [PATCH] =?UTF-8?q?454:=E5=9B=9B=E6=95=B0=E7=9B=B8=E5=8A=A0=20II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/leetcode/editor/cn/FourSumIi.java | 62 +++++++++++++++++++ .../editor/cn/doc/content/FourSumIi.md | 22 +++++++ 2 files changed, 84 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/FourSumIi.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/FourSumIi.md diff --git a/src/main/java/leetcode/editor/cn/FourSumIi.java b/src/main/java/leetcode/editor/cn/FourSumIi.java new file mode 100644 index 0000000..0631444 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/FourSumIi.java @@ -0,0 +1,62 @@ +//给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[ +//l] = 0。 +// +// 为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 。所有整数的范围在 -2²⁸ 到 2²⁸ - 1 之间,最 +//终结果不会超过 2³¹ - 1 。 +// +// 例如: +// +// +//输入: +//A = [ 1, 2] +//B = [-2,-1] +//C = [-1, 2] +//D = [ 0, 2] +// +//输出: +//2 +// +//解释: +//两个元组如下: +//1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0 +//2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0 +// +// Related Topics 数组 哈希表 👍 408 👎 0 + +package leetcode.editor.cn; + +import java.util.HashMap; +import java.util.Map; + +//454:四数相加 II +class FourSumIi { + public static void main(String[] args) { + //测试代码 + Solution solution = new FourSumIi().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { + Map map1 = new HashMap<>(); + Map map2 = new HashMap<>(); + int size = nums1.length; + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + map1.put(nums1[i] + nums2[j], map1.getOrDefault(nums1[i] + nums2[j], 0) + 1); + map2.put(nums3[i] + nums4[j], map2.getOrDefault(nums3[i] + nums4[j], 0) + 1); + } + } + int count = 0; + for (int key : map1.keySet()) { + if (map2.containsKey(-key)) { + count += map1.get(key) * map2.get(-key); + } + } + return count; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/FourSumIi.md b/src/main/java/leetcode/editor/cn/doc/content/FourSumIi.md new file mode 100644 index 0000000..6c7f496 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/FourSumIi.md @@ -0,0 +1,22 @@ +

给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0

+ +

为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 。所有整数的范围在 -228 到 228 - 1 之间,最终结果不会超过 231 - 1 。

+ +

例如:

+ +
+输入:
+A = [ 1, 2]
+B = [-2,-1]
+C = [-1, 2]
+D = [ 0, 2]
+
+输出:
+2
+
+解释:
+两个元组如下:
+1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
+2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
+
+
Related Topics
  • 数组
  • 哈希表

  • 👍 408
  • 👎 0
  • \ No newline at end of file