问题描述:
image.png输入:[4,3,2,7,8,2,3,1]
输出:[5,6]
代码示例一:
class Solution(object):
def findDisappearedNumbers(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
list = [1]*len(nums)
result = []
for i in range(len(nums)):
list[nums[i] - 1] = 0
for i in range(len(nums)):
if list[i] == 1:
result.append(i+1)
return result
代码示例二:不占用额外内存
class Solution(object):
def findDisappearedNumbers(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
result = []
for i in range(len(nums)):
count = abs(nums[i])
nums[count - 1] = - abs(nums[count - 1])
for i in range(len(nums)):
if nums[i] > 0:
result.append(i+1)
return result
网友评论