Python: missing number is intentional summary minus actual summary


  • 3
    B
    class Solution(object):
        def missingNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            n = len(nums)
            return n * (n + 1) / 2 - sum(nums)

  • 1
    class Solution(object):
        def missingNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            N = len(nums)
            sum = (1 + N) * N // 2
            for x in nums:
                sum -= x
            return sum

Log in to reply
 

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