C++ simple solution use index


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

Log in to reply
 

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