Python 1 line O(n) solution 48 ms beats 93%

  • 0
    class Solution(object):
        def missingNumber(self, nums):
            return (len(nums) + 1) * len(nums) / 2 - sum(nums)

    The idea is that given N, the total sum of 0,1,2,...N is always (N+1) * N / 2
    So the difference between sum(nums) and the total sum would be the missing number!

Log in to reply

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