zoukankan      html  css  js  c++  java
  • 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]),最后判断下下标对应的值不是负数就行
    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
    还蛮费脑力的这个题,
  • 相关阅读:
    Git
    Entropy, relative entropy and mutual information
    2021.5.3 团队冲刺第六天
    2021.5.2 团队冲刺第五天
    2021.5.1 团队冲刺第四天
    2021.4.30 团队冲刺第三天
    2021.4.29 团队冲刺第二天
    2021.4.28 团队冲刺第一天
    2021.4.27
    2021.4.26
  • 原文地址:https://www.cnblogs.com/whatyouthink/p/13227721.html
Copyright © 2011-2022 走看看