-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.cpp
More file actions
71 lines (58 loc) · 1.77 KB
/
15.cpp
File metadata and controls
71 lines (58 loc) · 1.77 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Written by Nitin Kumar Maharana
* nitin.maharana@gmail.com
*/
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
vector<int> tempResult;
int first, tempValue;
int len, l, r;
len = nums.size();
sort(nums.begin(), nums.end());
for(int i = 0; i < len-2;)
{
l = i+1;
r = len-1;
first = -1 * nums[i];
while(l < r)
{
if(nums[l]+nums[r] == first)
{
tempResult.push_back(nums[i]);
tempResult.push_back(nums[l]);
tempResult.push_back(nums[r]);
result.push_back(tempResult);
tempResult.clear();
tempValue = nums[l];
while(l < r)
{
l++;
if(nums[l] != tempValue)
break;
}
tempValue = nums[r];
while(l < r)
{
r--;
if(nums[r] != tempValue)
break;
}
}
else if(nums[l]+nums[r] > first)
r--;
else
l++;
}
tempValue = nums[i];
while(i < len-2)
{
i++;
if(nums[i] != tempValue)
break;
}
}
return result;
}
};