3-line and 4-line short C++ solutions


  • 0
        vector<int> findDuplicates(vector<int>& nums) {
          vector<int> res;
          for(int x : nums) if((nums[abs(x)-1]*=-1) > 0) res.push_back(abs(x));
          return res;
        }
    

    or

        vector<int> findDuplicates(vector<int>& nums) {
            vector<int> res; int k = 0;
            for (int& x:nums) while (x != nums[x-1]) swap(x, nums[x-1]);
            for (int& x:nums) if (x != ++k) res.push_back(x);
            return res;        
        }
    

Log in to reply
 

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