Skip to content

Latest commit

 

History

History
50 lines (43 loc) · 1.18 KB

File metadata and controls

50 lines (43 loc) · 1.18 KB

561. 数组拆分I

https://leetcode-cn.com/problems/array-partition-i/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-08-16 09:49:53
 * @LastEditTime: 2020-08-16 09:50:12
 * @Description: https://leetcode-cn.com/problems/array-partition-i/
 * @FilePath: \leetcode-googtech\#561. Array Partition I\Solution.java
 * @WebSite: https://algorithm.show/
 */

class Solution {

    //其实就是把从a1到an数组下标为奇数的数都加起来
    public int arrayPairSum(int[] nums) {
        Arrays.sort(nums);
        int sum = 0;
        for(int i = 0; i < nums.length; i += 2) {
            sum += nums[i];
        }
        return sum;
    }
}

Python

'''
Author: Goog Tech
Date: 2020-08-16 09:49:58
LastEditTime: 2020-08-16 09:51:47
Description: https://leetcode-cn.com/problems/array-partition-i/
FilePath: \leetcode-googtech\#561. Array Partition I\Solution.py
WebSite: https://algorithm.show/
'''

class Solution(object):

    # 其实就是把从a1到an数组下标为奇数的数都加起来
    def arrayPairSum(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        return sum(sorted(nums)[::2]) # seq[start:end:step]