0442.cpp (360B)
1 class Solution { 2 public: 3 vector<int> findDuplicates(vector<int> &nums) { 4 vector<int> res; 5 for (int i = 0; i < nums.size(); i++) { 6 int idx = abs(nums[i]) - 1; 7 if (nums[idx] < 0) 8 res.push_back(idx + 1); 9 else 10 nums[idx] = -nums[idx]; 11 } 12 13 return res; 14 } 15 };