Original link: https://leetcode-cn.com/problems/array-partition-i/
topic:
Thinking Analysis: This question is given a length 2n array, then these will be divided into n number of pairs,
1. The array elements in ascending order.
2 through the array, the subscript of the array elements is accumulated even.
3. Finally, return the results accumulated.
Source:
class Solution {
public int arrayPairSum(int[] nums) {
//insertSort(nums);
Arrays.sort(nums);
int sum = 0;
for(int i = 0;i < nums.length;i+=2){
sum+=nums[i];
}
return sum;
}
public static int[] insertSort(int[] nums){
int e;
int j;
for(int i = 1;i < nums.length;i++){
e = nums[i];
for(j = i;j > 0 && nums[j - 1] > e;j--){
nums[j] = nums[j - 1];
}
nums[j] = e;
}
return nums;
}
}