- 1

why this code not working, it always show duplicates how to avoid it

class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> ans; vector<int>temp; sort(nums.begin(), nums.end()); int n= nums.size(); for(int i= 0;i<n-2;i++) { for(int j= i+1;j<n-1;j++) { for(int k= j+1;k<n;k++) { temp.clear(); if(nums[i] + nums[j] + nums[k]==0) { temp.push_back(nums[i]); temp.push_back(nums[j]); temp.push_back(nums[k]); } if(temp.size()!= 0) { ans.push_back(temp); } } } } return ans; } };

17th Mar 2022, 3:20 PM
Praveen
Praveen - avatar
1 Respuesta
+ 2
I don't know much c++ however if you don't want duplicates I would recommend using a set.
17th Mar 2022, 3:59 PM
Isaac Nelson
Isaac Nelson - avatar