diff --git a/solution/015.3Sum/solution.cpp b/solution/015.3Sum/solution.cpp new file mode 100644 index 0000000000000..e05f7020e3679 --- /dev/null +++ b/solution/015.3Sum/solution.cpp @@ -0,0 +1,89 @@ +class Solution { +public: + vector > threeSum(vector &num) { + return threeSumGen(num, 0); + } + + vector > threeSumGen(vector &num, int target) { + vector> allSol; + if(num.size()<3) return allSol; + sort(num.begin(),num.end()); + + for(int i=0; i0 && num[i]==num[i-1]) continue; // 去重 + + int left=i+1, right=num.size()-1; + + while(left sol; + sol.push_back(num[i]); + sol.push_back(num[left]); + sol.push_back(num[right]); + allSol.push_back(sol); + left++; + right--; + while(num[left]==num[left-1]) left++; + while(num[right]==num[right+1]) right--; + } + else if(curSum> threeSum(vector& nums) { +// sort(nums.begin(), nums.end()); +// if (nums.size() <= 2 || nums[0] > 0) return{}; +// set> res; +// for (int i = 0; i < nums.size(); i++) +// { +// int target = 0 - nums[i]; +// int m = 0, n = nums.size() - 1; +// while (m < n) +// { +// while (true) +// { +// if (m >= n) break; +// if (m == i) +// { +// m++; +// continue; +// } +// if (n == i) +// { +// n--; +// continue; +// } +// if (nums[m] + nums[n] == target) { +// vector s = { nums[m++], nums[i], nums[n] }; +// sort(s.begin(), s.end()); +// res.insert(s); +// break; +// } +// if (nums[m] + nums[n] > target) n--; +// if (nums[m] + nums[n] < target) m++; +// } +// } + +// } +// vector> fres(res.begin(), res.end()); +// return fres; +// } +}; + +// accelerate the cin process. +int x = []() { + std::ios::sync_with_stdio(false); + cin.tie(NULL); + return 0; +}();