Python solution O(n) without extra space with explanation

  • 0

    Try to put every integer into its position since a[i] < n with "pointer" chasing

    def findDisappearedNumbers(self, nums):
        for i, v in enumerate(nums):
            while nums[v-1] != v:
                nums[v-1], v = v, nums[v-1]
        ans = [i+1 for i, v in enumerate(nums) if i+1 != v] 
        return ans

Log in to reply

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