LeetCode 442 – Python 3 (Week 4 – 03)

Solution 1

class Solution:
    def findDuplicates(self, nums: List[int]) -> List[int]:
        res = []
        for num in nums:
            index = abs(num) - 1
            if nums[index] > 0:
                nums[index] = - nums[index]
            else:
                res.append(index+1)
        
        return res
        

Time complexity is O(n). Space complexity is O(1).

Leave a comment

Design a site like this with WordPress.com
Get started