File tree 2 files changed +16
-4
lines changed
2 files changed +16
-4
lines changed Original file line number Diff line number Diff line change @@ -123,6 +123,9 @@ public:
123
123
### Java:
124
124
版本一:使用HashSet
125
125
``` Java
126
+ // 时间复杂度O(n+m+k) 空间复杂度O(n+k)
127
+ // 其中n是数组nums1的长度,m是数组nums2的长度,k是交集元素的个数
128
+
126
129
import java.util.HashSet ;
127
130
import java.util.Set ;
128
131
@@ -145,8 +148,15 @@ class Solution {
145
148
}
146
149
147
150
// 方法1:将结果集合转为数组
148
-
149
- return resSet. stream(). mapToInt(x - > x). toArray();
151
+ return res. stream(). mapToInt(Integer :: intValue). toArray();
152
+ /**
153
+ * 将 Set<Integer> 转换为 int[] 数组:
154
+ * 1. stream() : Collection 接口的方法,将集合转换为 Stream<Integer>
155
+ * 2. mapToInt(Integer::intValue) :
156
+ * - 中间操作,将 Stream<Integer> 转换为 IntStream
157
+ * - 使用方法引用 Integer::intValue,将 Integer 对象拆箱为 int 基本类型
158
+ * 3. toArray() : 终端操作,将 IntStream 转换为 int[] 数组。
159
+ */
150
160
151
161
// 方法2:另外申请一个数组存放setRes中的元素,最后返回数组
152
162
int [] arr = new int [resSet. size()];
538
548
<a href =" https://programmercarl.com/other/kstar.html " target =" _blank " >
539
549
<img src =" ../pics/网站星球宣传海报.jpg " width =" 1000 " />
540
550
</a >
551
+
Original file line number Diff line number Diff line change @@ -212,13 +212,14 @@ public class Main {
212
212
int horizontalCut = 0 ;
213
213
for (int i = 0 ; i < n; i++ ) {
214
214
horizontalCut += horizontal[i];
215
- result = Math . min(result, Math . abs(sum - 2 * horizontalCut));
215
+ result = Math . min(result, Math . abs((sum - horizontalCut) - horizontalCut));
216
+ // 更新result。其中,horizontalCut表示前i行的和,sum - horizontalCut表示剩下的和,作差、取绝对值,得到题目需要的“A和B各自的子区域内的土地总价值之差”。下同。
216
217
}
217
218
218
219
int verticalCut = 0 ;
219
220
for (int j = 0 ; j < m; j++ ) {
220
221
verticalCut += vertical[j];
221
- result = Math . min(result, Math . abs(sum - 2 * verticalCut));
222
+ result = Math . min(result, Math . abs(( sum - verticalCut) - verticalCut));
222
223
}
223
224
224
225
System . out. println(result);
You can’t perform that action at this time.
0 commit comments