C++ Solution: O(n) Count Sort


  • 0

    Use count sort to find disappeared numbers.

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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.