Python solution without using extra space but O(nlgn)


  • 1
    class Solution(object):
        def findDisappearedNumbers(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """
            res = []
            nums.sort()
            nums.insert(0, 0)
            nums.append(len(nums))
            i, diff = 1, 0
            while i < len(nums):
                if nums[i] - nums[i - 1] > 1:
                    diff = nums[i] - nums[i - 1]
                    cur = nums[i - 1] + 1
                    while cur != nums[i]:
                        res.append(cur)
                        cur += 1
                i += 1
            return res
    

Log in to reply
 

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