Skip to content

Latest commit

 

History

History
52 lines (35 loc) · 1.29 KB

442-find-all-duplicates-in-an-array.md

File metadata and controls

52 lines (35 loc) · 1.29 KB

442. Find All Duplicates in an Array - 数组中重复的数据

给定一个整数数组 a,其中1 ≤ a[i] ≤ nn为数组长度), 其中有些元素出现两次而其他元素出现一次

找到所有出现两次的元素。

你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗?

示例:

输入:
[4,3,2,7,8,2,3,1]

输出:
[2,3]

题目标签:Array

题目链接:LeetCode / LeetCode中国

题解

Language Runtime Memory
python3 200 ms N/A
class Solution:
    def findDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        tmp = [False] * len(nums)
        res = []
        for num in nums:
            if tmp[num-1]:
                res.append(num)
            else:
                tmp[num-1] = True
        return res