Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dab25be

Browse files
Merge pull request youngyangyang04#2870 from curforever/master
修改leetcode-master\problems\kamacoder0044円.开发商购买土地.md Java版本、修改leetcode-master\problems0349円.两个数组的交集.md Java版本
2 parents e8a10ab + cb5b6e5 commit dab25be

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

‎problems/0349.两个数组的交集.md‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public:
123123
### Java:
124124
版本一:使用HashSet
125125
```Java
126+
// 时间复杂度O(n+m+k) 空间复杂度O(n+k)
127+
// 其中n是数组nums1的长度,m是数组nums2的长度,k是交集元素的个数
128+
126129
import java.util.HashSet;
127130
import java.util.Set;
128131

@@ -145,8 +148,15 @@ class Solution {
145148
}
146149

147150
//方法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+
*/
150160

151161
//方法2:另外申请一个数组存放setRes中的元素,最后返回数组
152162
int[] arr = new int[resSet.size()];
@@ -538,3 +548,4 @@ end
538548
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
539549
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
540550
</a>
551+

‎problems/kamacoder/0044.开发商购买土地.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,14 @@ public class Main {
212212
int horizontalCut = 0;
213213
for (int i = 0; i < n; i++) {
214214
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各自的子区域内的土地总价值之差"。下同。
216217
}
217218

218219
int verticalCut = 0;
219220
for (int j = 0; j < m; j++) {
220221
verticalCut += vertical[j];
221-
result = Math.min(result, Math.abs(sum - 2* verticalCut));
222+
result = Math.min(result, Math.abs((sum - verticalCut) - verticalCut));
222223
}
223224

224225
System.out.println(result);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /