448. Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements of [1, n] inclusive that do not appear in this array.
Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
Example:
Input:[4,3,2,7,8,2,3,1]Output:[5,6]
给一个长度等于n的数组,出现的数的范围是1-n,问1-n里哪些数没出现在数组里。
on时间和o1空间,还蛮费脑力的这个题。
把值作为index,然后去把a[index - 1] = -abs(a[index - 1]),最后判断下下标对应的值不是负数就行语言方法
90615h17n
L8V0k
樱心美「私照曝光」不可不阅「精品图集」15232008.02.04 23-55-43
class Solution(object): def findDisappearedNumbers(self, nums): """ :type nums: List[int] :rtype: List[int] """ for value in nums: value = abs(value) if nums[value - 1] > 0: nums[value - 1] = -nums[value - 1] ans = [] for i in range(0, len(nums), 1): if nums[i] > 0: ans.append(i + 1) return ans
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。