标签:end || public sort bsp 3sum res return size
class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int>> res; if (nums.size() < 3) return res; for (int i = 0; i < nums.size() - 2; i++) { if (i > 0 && nums[i] == nums[i-1]) continue; int t = -nums[i]; for (int j = i+1, k = nums.size()-1; j < k; ) { if (nums[j] + nums[k] < t || j > i+1 && nums[j] == nums[j-1]) j++; else if (nums[j] + nums[k] > t || k < nums.size()-1 && nums[k] == nums[k+1]) k--; else if (nums[j] + nums[k] == t) { vector<int> temp; temp.push_back(nums[i]); temp.push_back(nums[j]); temp.push_back(nums[k]); res.push_back(temp); j++; k--; } } } return res; } };
标签:end || public sort bsp 3sum res return size
原文地址:https://www.cnblogs.com/JTechRoad/p/9994688.html