Skip to content

Commit 93b4777

Browse files
committed
solve problem Array Partition I
1 parent 8d7783b commit 93b4777

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ All solutions will be accepted!
1818
|617|[Merge Two Binary Trees](https://leetcode-cn.com/problems/merge-two-binary-trees/description/)|[java/py/js](./algorithms/MergeTwoBinaryTrees)|Easy|
1919
|226|[Invert Binary Tree](https://leetcode-cn.com/problems/invert-binary-tree/description/)|[java/py/js](./algorithms/InvertBinaryTree)|Easy|
2020
|693|[Binary Number With Alternating Bits](https://leetcode-cn.com/problems/binary-number-with-alternating-bits/description/)|[java/py/js](./algorithms/BinaryNumberWithAlternatingBits)|Easy|
21-
|108|[Convert Sorted Array To Binary Search Tree](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/description/)|[java/py/js](./algorithms/ConvertSortedArrayToBinarySearchTree)|Easy|
21+
|108|[Convert Sorted Array To Binary Search Tree](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/description/)|[java/py/js](./algorithms/ConvertSortedArrayToBinarySearchTree)|Easy|
22+
|561|[Array Partition I](https://leetcode-cn.com/problems/array-partition-i/description/)|[java/py/js](./algorithms/ArrayPartitionI)|Easy|

Diff for: algorithms/ArrayPartitionI/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Array Partition I
2+
Sort the array, then sum the odd term

Diff for: algorithms/ArrayPartitionI/Solution.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int arrayPairSum(int[] nums) {
3+
Arrays.sort(nums);
4+
int sum = 0;
5+
for (int i = 0; i < nums.length; i++) {
6+
if (i % 2 == 0) {
7+
sum += nums[i];
8+
}
9+
}
10+
return sum;
11+
}
12+
}

Diff for: algorithms/ArrayPartitionI/solution.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var arrayPairSum = function(nums) {
6+
return nums.sort((x, y) => y - x).filter((d, i) => i % 2 === 0).reduce((x, y) => x + y, 0)
7+
};

Diff for: algorithms/ArrayPartitionI/solution.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution(object):
2+
def arrayPairSum(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
return sum(sorted(nums)[::2])

0 commit comments

Comments
 (0)