My code
The problem
Problem in solving a problem
hello @Somasree
the question is asking u to print only unique triplets.
but ur solution will print duplicates as well.
Can you please correct my solution
i dont know java (i m ta for c++).
try this ->use set to store ur results
ok.
So i have written the same thing in C++.please now correct it and tell me where I am going wrong
check now->
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(),nums.end());
set<vector<int> > st;
for (int i = 0; i < nums.size(); i++) {
for (int j = i + 1; j < nums.size(); j++) {
for (int k = j + 1; k < nums.size(); k++) {
if (nums[i] + nums[j] + nums[k] == 0) {
st.insert({nums[i],nums[j],nums[k]});
}
}
}
}
vector<vector<int> > result;
for(auto k : st)
result.push_back(k);
return result;
}
};
used set to avoid duplicate entries.
but this will give tle.
try using two pointers approach.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.