2 lines Python


  • 0

    Just sorting the array first and return the sum of even index element.

    class Solution(object):
        def arrayPairSum(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            nums.sort()
            return sum(nums[i*2] for i in range(len(nums)//2))
    

  • 1
    E

    Hey found a simpler solution! :)
    Help me get to one liner?

    class Solution(object):

    def arrayPairSum(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.sort()
        return sum(nums[::2])

  • 0

    @ecdubs
    Basically same thing.

    def arrayPairSum(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        return sum(sorted(nums)[::2])
    

  • 0
    E

    @realisking Oh im just learning python today when I tried that I messed up the brackets thanks


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.