Skip to content

Commit 61e41ef

Browse files
committed
feat: add the solution of Array Partition I(561) with Java.
1 parent 751b0bb commit 61e41ef

File tree

3 files changed

+70
-24
lines changed

3 files changed

+70
-24
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
| [122][122-question] | [Best Time to Buy and Sell Stock II][122-tips] | [][122-java] | | |
5959
| [226][226-question] | [Invert Binary Tree][226-tips] | [][226-java] | [][226-js] | |
6060
| [543][543-question] | [Diameter of Binary Tree][543-tips] | [][543-java] | | |
61-
| [561][561-question] | [Array Partition I][561-tips] | | [][561-js] | |
61+
| [561][561-question] | [Array Partition I][561-tips] | [][561-java] | [][561-js] | |
6262
| [643][643-question] | [Maximum Average Subarray I][643-tips] | | [][643-js] | |
6363
| [645][645-question] | [Set Mismatch][645-tips] | | [][645-js] | |
6464
| [653][653-question] | [Two Sum IV - Input is a BST][653-tips] | | [][653-js] | |
@@ -443,6 +443,7 @@
443443
[226-java]: ./src/_226/Solution.java
444444
[543-java]: ./src/_543/Solution.java
445445
[554-java]: ./src/_554/Solution.java
446+
[561-java]: ./src/_561/Solution.java
446447
[728-java]: ./src/_728/Solution.java
447448
[771-java]: ./src/_771/Solution.java
448449
[804-java]: ./src/_804/Solution.java

src/_561/Solution.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package _561;
2+
3+
public class Solution {
4+
5+
public int arrayPairSum(int[] nums) {
6+
// 使用桶排序
7+
// 创建map数组,map数组下标对应nums数组内的数字,存的是对应数字的数量
8+
int[] map = new int[20001];
9+
for (int num : nums) {
10+
map[num + 10000]++;
11+
}
12+
13+
// 遍历,取出奇数位的数并相加即可
14+
int ans = 0;
15+
boolean odd = false;
16+
for (int i = 0; i < map.length; i++) {
17+
while (map[i] != 0) {
18+
if (odd = !odd) {
19+
ans += i - 10000;
20+
}
21+
map[i]--;
22+
}
23+
}
24+
return ans;
25+
}
26+
27+
public static void main(String[] args) {
28+
Solution solution = new Solution();
29+
System.out.println(solution.arrayPairSum(new int[]{1, 4, 3, 2}));
30+
}
31+
}

tips/561/README.md

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,60 @@
1-
[xxxx][title]
1+
[Array Partition I][title]
22

33
## Description
4-
// 抄题目
5-
4+
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.
65

76
**Example:**
87

98
```
10-
// 抄Example
9+
Input: [1,4,3,2]
10+
11+
Output: 4
12+
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
1113
```
1214

1315
**Note:**
14-
// Note
1516

16-
**Tags:** // tags
17+
1. **n** is a positive integer, which is in the range of [1, 10000].
18+
2. All the integers in the array will be in the range of [-10000, 10000].
19+
20+
**Tags:** [Array](https://leetcode.com/tag/array/)
1721

1822

1923
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
2324

24-
```
25-
```javascript
25+
题目要求将所给数组两两分组为(a1, b1), (a2, b2), ..., (an, bn),使得min(ai, bi)的和最大。
2626

27-
```
27+
了解题目可知,对数组进行排序,取出奇数位的数字相加即可。题目限制数组中的数字范围为[-10000, 10000],适合使用桶排序提高排序的速度。
2828

29-
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
3129
```java
32-
33-
```
34-
35-
## 思路 3
36-
// 贴一些关键代码,说一些解题思路
37-
```kotlin
38-
30+
public class Solution {
31+
public int arrayPairSum(int[] nums) {
32+
// 使用桶排序
33+
// 创建map数组,map数组下标对应nums数组内的数字,存的是对应数字的数量
34+
int[] map = new int[20001];
35+
for (int num : nums) {
36+
map[num + 10000]++;
37+
}
38+
39+
// 遍历,取出奇数位的数并相加即可
40+
int ans = 0;
41+
boolean odd = false;
42+
for (int i = 0; i < map.length; i++) {
43+
while (map[i] != 0) {
44+
if (odd = !odd) {
45+
ans += i - 10000;
46+
}
47+
map[i]--;
48+
}
49+
}
50+
return ans;
51+
}
52+
}
3953
```
4054

4155
## 结语
42-
56+
4357
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
4458

45-
[title]: https://leetcode.com/problems/xxxx
59+
[title]: https://leetcode.com/problems/array-partition-i/description/
4660
[ls]: https://github.com/SDE603/LeetCode-Solution

0 commit comments

Comments
 (0)