-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 2143 ms (5.01%), Space: 291.6 MB (7.08%) - LeetHub
- Loading branch information
1 parent
66cd40f
commit 5cc1501
Showing
1 changed file
with
23 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,38 @@ | ||
class Solution { | ||
public: | ||
vector<vector<int>> threeSum(vector<int>& nums) { | ||
vector<vector<int>> res; | ||
int n = nums.size(); | ||
|
||
if(nums.size() < 3) | ||
return res; | ||
|
||
set<vector<int>> res; | ||
sort(begin(nums), end(nums)); | ||
|
||
for(int i = 0; i < nums.size(); i++){ | ||
int t = nums[i], l = i + 1, r = nums.size() - 1; | ||
for(int i = 0; i < nums.size(); i++) { | ||
int temp = nums[i]; | ||
int l = 0, r = nums.size() - 1; | ||
|
||
while(l < r){ | ||
int sum = t + nums[l] + nums[r]; | ||
if(sum == 0){ | ||
res.push_back({t, nums[l], nums[r]}); | ||
while(l <r && nums[l] == nums[l + 1]) l++; | ||
while(l < r && nums[r] == nums[r - 1]) r--; | ||
|
||
while(l < r) { | ||
int sum = nums[l] + nums[r] + temp; | ||
// cout<<<<temp<<" "<<nums[l]sum<<endl; | ||
if(sum == 0 && l != i && r != i) { | ||
vector<int> temp_v = {nums[l], nums[r], temp}; | ||
sort(begin(temp_v), end(temp_v)); | ||
res.insert(temp_v); | ||
l++; | ||
r--; | ||
}else if(sum > 0){ | ||
r--; | ||
}else{ | ||
} else if(sum < 0) { | ||
l++; | ||
} else if (sum > 0) { | ||
r--; | ||
} else if(l == i || r == i) { | ||
break; | ||
} | ||
} | ||
while(i + 1 < n && nums[i] == nums[i + 1]) i++; | ||
} | ||
|
||
return res; | ||
vector<vector<int>> res_v; | ||
|
||
for(vector<int> v: res) { | ||
res_v.push_back(v); | ||
} | ||
|
||
return res_v; | ||
} | ||
}; |