Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- using pi = pair <int, int>;
- vector < vector <int>> ans;
- int pos[200010], neg[200010], zero;
- pi piz = {0, 0};
- vector<vector<int>> threeSum(vector<int>& nums) {
- int len = nums.size();
- if(len < 3) return ans;
- sort(nums.begin(), nums.end());
- for(auto i: nums){
- if(i == 0) zero ++;
- else if(i > 0) pos[i] ++;
- else neg[-i] ++;
- }
- for(int i=0;i<len;i++){
- for(int j=i+1;j<len;j++){
- int a = nums[i], b = nums[j], c = a + b;
- if(a < 0 and b < 0 and pos[-c] != 0){
- ans.push_back({a, b, -c});
- }
- if(a > 0 and b > 0 and neg[c] != 0){
- ans.push_back({-c, a, b});
- }
- }
- }
- if(zero >= 1){
- for(int i=1;i<=100000;i++){
- if(pos[i] != 0 and neg[i] != 0)
- ans.push_back({-i, 0, i});
- }
- }
- if(zero >= 3) ans.push_back({0, 0, 0});
- sort(ans.begin(), ans.end());
- ans.resize( unique(ans.begin(), ans.end()) - ans.begin() );
- return ans;
- }
- };
Add Comment
Please, Sign In to add comment