One-liner python


  • 1
    F
    class Solution(object):
    def missingNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        return sum([i for i in xrange(len(nums)+1)])-sum(nums)

  • 0
    H

    similar idea
    class Solution(object):
    def missingNumber(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    return (0+len(nums)) * (len(nums)+1) / 2 - sum(nums)


  • 0
    F

    Of course, that is the mathematical way of expressing the sum function!


Log in to reply
 

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