美文网首页
442. Find All Duplicates in an A

442. Find All Duplicates in an A

作者: 阿团相信梦想都能实现 | 来源:发表于2016-12-11 16:16 被阅读0次
use the position of the array as hash, use +- to mark state
class Solution(object):
    def findDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        res=[]
        for num in nums:
            if nums[abs(num)-1]<0:
                res.append(abs(num))
            else: 
                nums[abs(num)-1]*=-1
        return res
            

相关文章

网友评论

      本文标题:442. Find All Duplicates in an A

      本文链接:https://www.haomeiwen.com/subject/nkjpmttx.html