Non-math Non-bit, normal solution O(N)


  • 1
    P
    class Solution(object):
        def missingNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            n = len(nums)
            i = 0
            while i < n:
                cur = nums[i]
                if cur == n or cur == i:
                    i+=1
                    continue
                tmp = nums[cur]
                nums[cur] = cur
                nums[i] = tmp
            i = 0
            while i<n:
                if i != nums[i]:
                    return i
                i +=1
        
            return n
    

Log in to reply
 

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