Beats 97% C++ Submissions, O(n) Solution


  • 0
    L

    RTFSC

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

Log in to reply
 

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