From a7ad0cd812649e0cf1928d9c4e54bc4369588a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Sat, 11 Jan 2025 19:18:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9leetcode-master\problems\?= =?UTF-8?q?kamacoder\0044.=E5=BC=80=E5=8F=91=E5=95=86=E8=B4=AD=E4=B9=B0?= =?UTF-8?q?=E5=9C=9F=E5=9C=B0.md=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/kamacoder/0044.开发商购买土地.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/problems/kamacoder/0044.开发商购买土地.md b/problems/kamacoder/0044.开发商购买土地.md index 739e2cad..efad56da 100644 --- a/problems/kamacoder/0044.开发商购买土地.md +++ b/problems/kamacoder/0044.开发商购买土地.md @@ -212,13 +212,14 @@ public class Main { int horizontalCut = 0; for (int i = 0; i < n; i++) { horizontalCut += horizontal[i]; - result = Math.min(result, Math.abs(sum - 2 * horizontalCut)); + result = Math.min(result, Math.abs((sum - horizontalCut) - horizontalCut)); + // 更新result。其中,horizontalCut表示前i行的和,sum - horizontalCut表示剩下的和,作差、取绝对值,得到题目需要的“A和B各自的子区域内的土地总价值之差”。下同。 } int verticalCut = 0; for (int j = 0; j < m; j++) { verticalCut += vertical[j]; - result = Math.min(result, Math.abs(sum - 2 * verticalCut)); + result = Math.min(result, Math.abs((sum - verticalCut) - verticalCut)); } System.out.println(result); From cb5b6e524197e1e2f66bcd3b157b37bfbc726803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Sun, 12 Jan 2025 11:54:23 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9leetcode-master\problems\?= =?UTF-8?q?0349.=E4=B8=A4=E4=B8=AA=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4?= =?UTF-8?q?=E9=9B=86.md=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0349.两个数组的交集.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 77dfc50a..93fa0931 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -123,6 +123,9 @@ public: ### Java: 版本一:使用HashSet ```Java +// 时间复杂度O(n+m+k) 空间复杂度O(n+k) +// 其中n是数组nums1的长度,m是数组nums2的长度,k是交集元素的个数 + import java.util.HashSet; import java.util.Set; @@ -145,8 +148,15 @@ class Solution { } //方法1:将结果集合转为数组 - - return resSet.stream().mapToInt(x -> x).toArray(); + return res.stream().mapToInt(Integer::intValue).toArray(); + /** + * 将 Set 转换为 int[] 数组: + * 1. stream() : Collection 接口的方法,将集合转换为 Stream + * 2. mapToInt(Integer::intValue) : + * - 中间操作,将 Stream 转换为 IntStream + * - 使用方法引用 Integer::intValue,将 Integer 对象拆箱为 int 基本类型 + * 3. toArray() : 终端操作,将 IntStream 转换为 int[] 数组。 + */ //方法2:另外申请一个数组存放setRes中的元素,最后返回数组 int[] arr = new int[resSet.size()]; @@ -538,3 +548,4 @@ end +