448. [LeetCode]Find All Numbers Disappeared in an Array

来源:互联网 发布:电子地图绘制软件 编辑:程序博客网 时间:2024/05/21 09:09

原文地址

class Solution(object):    def findDisappearedNumbers(self, nums):        """        :type nums: List[int]        :rtype: List[int]        """        # For each number i in nums,        # we mark the number that i points as negative.        # Then we filter the list, get all the indexes        # who points to a positive number        for i in range(len(nums)):            index = abs(nums[i]) - 1            nums[index] = - abs(nums[index])        return [i + 1 for i in range(len(nums)) if nums[i] > 0]

这道题有两个注意点:
1. 可以使用正负号,在不改变数列中,数的大小的情况下,对数列中的数,进行标记
2. 注意返回值的形式

0 0