分享

448. Find All Numbers Disappeared in an Array

 昵称70680357 2020-07-02

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]),最后判断下下标对应的值不是负数就行
语言 方法
9061 5h17n
L8V0k
  • 樱心美「私照曝光」不可不阅「精品图集」
  • 1523 2008.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

      本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
      转藏 分享 献花(0

      0条评论

      发表

      请遵守用户 评论公约

      类似文章 更多