Python Solution


  • 0
    B

    Takes O(N) time and O(N + N) space. Please correct me if my time and space analysis are wrong.

    class Solution(object):
        def findDuplicates(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """
            d = {}                    # used for key lookups only
            ret = []                  # store duplicates
            for num in nums:
                if num not in d:
                    d[num] = True     # store a bool to minimize space
                else: 
                    ret.append(num)
            return ret
    

    Thank you.


Log in to reply
 

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