Simple C++ solution O(n) time and O(1) space complexity


  • 0
    M
        vector<int> findDuplicates(vector<int>& nums) {
            
            vector<int> result;
            
            for(int i=0;i<nums.size();i++)
            {
                if(nums[abs(nums[i])-1] > 0)
                    nums[abs(nums[i])-1] *= -1;
                else
                    result.push_back(abs(nums[i]));
            }
            
            return result;
        }
    

Log in to reply
 

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