Easy Python solution. O(1) space and O(n) time.


  • 0
    L

    Just use the numbers to mark the array by adding it's length. If an index is not marked, then that was a missing number. If it's marked twice, then that was the duplicated number.

        def findErrorNums(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """
            n = len(nums)
            
            for num in nums :
                nums[(num - 1) % n] += n
                
            result = [""] * 2
            for i, num in enumerate(nums) :
                if nums[i] > 2*n : result[0] = i + 1
                elif nums[i] <= n : result[1] = i + 1
            return result
    

Log in to reply
 

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