My 52ms simple python solution with math


  • 0
    V
    class Solution(object):
        def missingNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            n = len(nums) 
            res = n*(n+1) / 2 # get the sum of the total 0~n values where there is no missing value
            for i in nums: # subtract each item in the list and the last remaining value is the result
                res -= i
            return res

Log in to reply
 

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