leecode 448 找到所有数组中消失的数字

描述

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

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

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

示例:

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

输出:
[5,6]

思路

将数组元素视作索引,出现过的索引对应的数组元素取反,最后数组内为正的元素下标为结果。

class Solution {
public:
    vector<int> findDisappearedNumbers(vector<int>& nums) {
        vector<int> res;
        for(int a:nums){
            int idx=abs(a)-1;
            if(nums[idx]>0)nums[idx]*=(-1);
        }
        for(int i=0;i<nums.size();i++){
            if(nums[i]>0)res.push_back(i+1);
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/yrk0556/article/details/89423330