您的位置:首页 > 移动开发

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

2016-12-29 12:01 465 查看
原文地址

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. 注意返回值的形式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode