Leetcode 分享筆記

442. Find All Duplicates in an Array
等級: Easy

原始題目連結請點此

leetcode

442. Find All Duplicates in an Array

class Solution(object):
    def findDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        new = set() # 因為set裡面的值必須是唯一的,因此判斷裡面是否包含重複值的時候很快
        ans = []
        for num in nums:
            if num not in new:
                new.add(num)
            else:
                ans.append(num)
        return ans