BM54. 三数之和
描述
示例1
输入:
[-10,0,10,20,-10,-40]
输出:
[[-10,-10,20],[-10,0,10]]
示例2
输入:
[-2,0,1,1,2]
输出:
[[-2,0,2],[-2,1,1]]
示例3
输入:
[0,0]
输出:
[]
C++ 解法, 执行用时: 2ms, 内存消耗: 384KB, 提交时间: 2021-06-20
class Solution { public: vector<vector<int> > threeSum(vector<int> &num) { vector<vector<int>> ret = {}; int sz = num.size(); if (sz < 3) return ret; //先排序 sort(num.begin(), num.end()); //遍历数组 选为target 跳过重复 for (int i = 0; i < sz - 2; i++) { int l = i + 1; //左指针 int r = sz - 1; //右指针 int tar = -num[i]; //target while (l < r) { if (num[l] + num[r] > tar) { //太大了 小一点 r--; }else if (num[l] + num[r] < tar) { //太小了 大一点 l++; }else { //正好找到家和为-tar的组合 vector<int> tmp_ret = {num[i], num[l], num[r]}; ret.push_back(tmp_ret); //使l,r跳过重复项 while (r - 1 > l && num[r - 1] == num[r]) r--; while (l + 1 < r && num[l + 1] == num[l]) l++; r--, l++; } } //遍历i时防止重复项 while (i + 1 < sz - 2 && num[i] == num[i + 1]) i++; } return ret; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 388KB, 提交时间: 2021-07-20
class Solution { public: vector<vector<int> > threeSum(vector<int> &num) { vector<vector<int> > res; sort(num.begin(),num.end()); int n=num.size(); for(int i=0;i+2<n;i++){ int j=i+1; int k=n-1; int target=-num[i]; while(j<k){ if(num[j]+num[k]>target) k--; else if(num[j]+num[k]<target) j++; else{ vector<int> tmp={num[i],num[j],num[k]}; res.push_back(tmp); while(j<k && num[j]==num[j+1]) j++; while(k>j && num[k]==num[k-1]) k--; j++,k--; } } while(i+2<n && num[i]==num[i+1]) i++; } return res; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 396KB, 提交时间: 2022-02-09
class Solution { public: vector<vector<int> > threeSum(vector<int> &num) { sort(num.begin(), num.end());//开始进行从小到大进行排序 vector<vector<int> > res; if (num.size() < 3) return res;//小于三个直接返回false 进行特判 for (int i=0;i<num.size()-2;i++) { int j=i+1; int k=num.size()-1; int target=-num[i];//预先减去遍历元素的大小 //双指针操作 while (j<k) { if (num[j]+num[k]>target) k--;//结果大于目标值 右指针左移 else if (num[j]+num[k]<target) j++;//结果小于目标值 左指针左移 else { vector<int> current = {num[i], num[j], num[k]}; res.push_back(current); //都是为了防止重复 while (j+1<k&&num[j+1]==num[j]) j++; while (k-1>j&&num[k-1]==num[k]) k--; j++; k--; } } //防止重复 while(i+1<num.size()-2&&num[i+1]==num[i]) i++; } return res; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 396KB, 提交时间: 2021-09-13
class Solution { public: vector<vector<int> > threeSum(vector<int> &nums) { int n=nums.size(); if(n<3)return {}; sort(nums.begin(),nums.end()); vector<vector<int> >ans; for(int i=0;i<n-2;++i){ if(nums[i]>0)break; if(i>=1&&nums[i]==nums[i-1])continue; int l=i+1; int r=n-1; while(l<r){ if(nums[i]+nums[l]+nums[r]==0){ ans.emplace_back(vector<int>{nums[i],nums[l],nums[r]}); l++; r--; while(l<r&&nums[l]==nums[l-1])++l; while(i<r&&nums[r]==nums[r+1])r--; } else if(nums[i]+nums[l]+nums[r]<0)++l; else r--; } } return ans; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 396KB, 提交时间: 2021-09-01
class Solution { public: vector<vector<int> > threeSum(vector<int> &num) { sort(num.begin(), num.end()); vector<vector<int>> vec; int len = num.size(), l, r; for(int i = 0; i < len; i++){ if(i != 0 && num[i]==num[i-1]) continue; int l = i + 1; int r = len - 1; while(l < r){ if(num[i] + num[l] + num[r] ==0){ vec.push_back({num[i], num[l], num[r]}); l++; r--; while(l < len && num[l] == num[l-1]) l++;//去重 while(r > l && num[r] == num[r+1]) r--; } else if(num[i]+num[l]+num[r]>0){ r--; } else{ l++; } } } return vec; } };