2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_561.java 1.16 KB
一键复制 编辑 原始数据 按行查看 历史
CG国斌 提交于 2020-03-15 20:56 . 新增 数组&字符串 题解
package com.hit.basmath.learn.array_and_string;
import java.util.Arrays;
/**
* 561. Array Partition I
* <p>
* 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.
* <p>
* Example 1:
* Input: [1,4,3,2]
* <p>
* Output: 4
* Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
* <p>
* Note:
* n is a positive integer, which is in the range of [1, 10000].
* All the integers in the array will be in the range of [-10000, 10000].
*/
public class _561 {
public int arrayPairSum(int[] nums) {
/**
* Use Arrays.sort() method let nums sort from small to big
*/
Arrays.sort(nums);
int aimSum = 0;
/**
* According to the topic of this question,
* we can know when nums sorted from small to big,
* only accumulate element of nums that index is 0、2、4 ... 2n that will do
*/
for (int i = 0; i < nums.length; i = i + 2) {
aimSum = aimSum + nums[i];
}
return aimSum;
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助