forked from Gogogoforit/LeetCode-Work
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path015. 3Sum.cpp
More file actions
53 lines (39 loc) · 1.46 KB
/
015. 3Sum.cpp
File metadata and controls
53 lines (39 loc) · 1.46 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
//https://leetcode.com/problems/3sum/discuss/7402/Share-my-AC-C%2B%2B-solution-around-50ms-O(N*N)-with-explanation-and-comments
//思路: 无序变有序
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> vv;
sort(nums.begin(),nums.end());
if(nums.size()<3)
return vv;
int i,left,right;
i = left = right = 0;
while(i<nums.size()) {
int target = -nums[i];
int left = i+1;
int right = nums.size()-1;
while(left<right) {
if(left!=right && nums[left]+nums[right]==target) {
vector<int> v(3, 0);
v[0] = nums[i];
v[1] = nums[left];
v[2] = nums[right];
vv.push_back(v);
while (left < right && nums[left] == v[1]) left++;
while (left < right && nums[right] == v[2]) right--;
}
if(nums[left]+nums[right]<target) {
left++;
}
if(nums[left]+nums[right]>target) {
right--;
}
}
while(i<nums.size() && nums[i] ==nums[i+1])
i++;
i++;
}
return vv;
}
};