Beats 4.25% of C++ solution :)


  • 0
    A
    class Solution {
    public:
        vector<int> findDisappearedNumbers(vector<int>& nums) {
            std::vector<int> result;
            
            for (int i=0; i<nums.size(); ++i) {
                if(nums[abs(nums[i])-1] > 0) { // if location is positive, make it negative
                    nums[abs(nums[i])-1] = 0-nums[abs(nums[i])-1];
                }
            }
            
            for (int i=0; i<nums.size(); ++i) {
                if (nums[i]>0) { // never found if positive
                    result.push_back(i+1);
                }
            }
            return result;
        }
    };

Log in to reply
 

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