leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0015.cpp (930B)


0 class Solution { 1 public: 2 vector<vector<int>> threeSum(vector<int> &num) { 3 sort(num.begin(), num.end()); 4 5 vector<vector<int>> res; 6 for (int i = 0; i < num.size();) { 7 int target = -num[i], start = i + 1, end = num.size() - 1; 8 9 while (start < end) { 10 int sum = num[start] + num[end]; 11 if (sum < target) 12 start++; 13 else if (sum > target) 14 end--; 15 else { 16 res.push_back({num[i], num[start], num[end]}); 17 while (start < end && num[start] == res.back()[1]) 18 start++; 19 while (start < end && num[end] == res.back()[2]) 20 end--; 21 } 22 } 23 for (i++; i < num.size() && num[i] == num[i - 1]; i++) 24 ; 25 } 26 27 return res; 28 } 29 };