forked from ravikartar/hacktober2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3sum.cpp
More file actions
32 lines (32 loc) · 712 Bytes
/
3sum.cpp
File metadata and controls
32 lines (32 loc) · 712 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int i =0;
int sum = 0;
int j=0;
int n = nums.size();
vector<int> ans;
vector<vector<int>> two;
while(j<n)
{
sum = sum+nums[j];
ans.push_back(nums[j]);
if((j-i+1)<3)
{
j++;
}
else if((j-i+1)==3)
{
if(sum==0)
{
two.push_back(ans);
}
ans.erase(ans.begin());
sum = sum-nums[i];
i++;
j++;
}
}
return two;
}
};