class Solution { public: vector<vector<int> > ThreeSum(int a[], int n, int target) { vector<vector<int> > ret; for (int i = 0; i < n; i++) { if(i>0 && a[i] == a[i - 1]) continue; int begin = i, end = n - 1; while (begin <= end) { int sum = a[i] + a[begin] + a[end]; if (sum == target) { vector<int> res = { a[i], a[begin], a[end] }; ret.push_back(res); begin++; end--; while (begin < end && a[begin] == a[begin - 1]) begin++; while (begin<end && a[end] == a[end + 1]) end--; } else if (sum>target) { end--; } else { begin++; } } } return ret; } };
时间: 2024-10-28 15:37:30