【leetcode】448. 找到所有数组中消失的数字(find-all-numbers-disappeared-in-an-array)(模拟)[简单]

2023-05-16

链接

https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/

耗时

解题:16 + 24 + 10 + 60 min
题解:11 + 15 + 14 + 9 min

题意

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

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

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

思路

id思路时间空间代码
1用 hash 表存一遍数组中的元素,然后从1到n检查一遍,将不在hash表中的元素放入结果数组 O ( n ) O(n) O(n) O ( n ) O(n) O(n)code1
2原地排序,然后从1到n遍历,同时遍历排序后的数组,并跳过数组中相同的相邻元素(相同元素最多出现2次,所以向后移一位即可),遍历地同时将数组中没有的元素放入结果数组 O ( n l o g n ) O(nlogn) O(nlogn) O ( 1 ) O(1) O(1)code2
3遍历一遍数组,如果 当前元素 和 下标+1 不等 并且 当前元素 和 下标是当前元素值-1的位置的元素 不等,则将 当前元素 和 下标是当前元素值-1的位置的元素 互换,直到有一个相等则继续遍历。最后扫描一遍数组,如果当前元素和当前位置下标+1不等,则将下标+1放入结果 O ( n ) O(n) O(n) O ( 1 ) O(1) O(1)code3
4遍历一遍数组,如果 当前元素不是-1 并且 下标是当前元素值-1的位置的元素也不是-1,则将 当前元素 和 下标是当前元素值-1的位置的元素 互换,并把下标是当前元素值-1的位置的元素变成-1。最后扫描一遍数组,如果当前元素不是-1,则将下标+1放入结果(2021/2/13) O ( n ) O(n) O(n) O ( 1 ) O(1) O(1)code4

AC代码

code1

class Solution {
public:
    vector<int> findDisappearedNumbers(vector<int>& nums) {
        unordered_set<int> se;
        for(auto num : nums) {
            se.insert(num);
        }
        int n = nums.size();
        vector<int> ans;
        for(int x = 1; x <= n; ++x) {
            if(se.find(x) != se.end()) continue;
            ans.push_back(x);
        }
        return ans;
    }
};

back to 思路

code2

class Solution {
public:
    vector<int> findDisappearedNumbers(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        vector<int> ans;
        int i = 0;
        for(int x = 1; x <= n; ++x) {
            if(i > 0 && i < n && nums[i] == nums[i-1]) i++;
            if(i == n) {
                ans.push_back(x);
                continue;
            }
            if(x == nums[i]) i++;
            else ans.push_back(x);
        }
        return ans;
    }
};

back to 思路

code3

class Solution {
public:
    vector<int> findDisappearedNumbers(vector<int>& nums) {
        int n = nums.size();
        for(int i = 0; i < n; ++i) {
            while(nums[i] != i+1 && nums[i] != nums[nums[i]-1]) {
                swap(nums[i], nums[nums[i]-1]);
            }
        }
        vector<int> ans;
        for(int i = 0; i < n; ++i) {
            if(nums[i] != i+1) {
                ans.push_back(i+1);
            }
        }
        return ans;
    }
};

back to 思路

code4

class Solution {
public:
    vector<int> findDisappearedNumbers(vector<int>& nums) {
        int n = nums.size();
        int i = 0;
        while(i < n) {
            if(nums[i] == -1 || nums[nums[i]-1] == -1) {
                i++;
            }
            else {
                int pos = nums[i]-1;
                swap(nums[nums[i]-1], nums[i]);
                nums[pos] = -1;
            }
        }
        vector<int> ans;
        for(int i = 0; i < n; ++i) {
            if(nums[i] != -1) {
                ans.push_back(i+1);
            }
        }
        return ans;
    }
};

back to 思路

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【leetcode】448. 找到所有数组中消失的数字(find-all-numbers-disappeared-in-an-array)(模拟)[简单] 的相关文章

随机推荐