给定一个范围在  1 ≤ a[i] ≤ nn = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次。

找到所有在 [1, n] 范围之间没有出现在数组中的数字。

您能在不使用额外空间且时间复杂度为O(n)的情况下完成这个任务吗? 你可以假定返回的数组不算在额外空间内。

示例:

输入:
[4,3,2,7,8,2,3,1]

输出:
[5,6]
class Solution(object):
    def findDisappearedNumbers(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        for num in nums:
            nums[abs(num)-1] = -abs(nums[abs(num)-1])
        return [i + 1 for i, a_num in enumerate(nums) if a_num > 0]

 




相关文章:

  • 2021-11-19
  • 2021-07-01
  • 2022-12-23
  • 2022-02-21
  • 2021-06-20
  • 2021-10-22
  • 2021-10-11
  • 2021-12-09
猜你喜欢
  • 2021-08-26
  • 2021-11-07
  • 2021-10-12
  • 2021-10-11
  • 2021-07-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案