赞
踩
给Krahet大佬跪了,写出来总是考虑不到细节有BUG。
- class Solution {
- public:
- vector<vector<int>> threeSum(vector<int>& nums) {
- vector<vector<int>> res;
- int n = nums.size();
- sort(nums.begin(),nums.end());
- for(int i = 0; i < n; ++i) {
- if(nums[i] > 0) break;
- if(i > 0 && nums[i] == nums[i-1]) continue;
- int left = i + 1,right = n - 1;
- while(left < right) {
- int sum = nums[i] + nums[left] +nums[right];
- if(sum < 0){
- do{++left;} while(nums[left] == nums[left-1] && left < right);
- }else if(sum > 0){
- do{--right;} while(nums[right] == nums[right+1] && left < right);
- }else{
- res.push_back({nums[i],nums[left],nums[right]});
- do{++left;} while(nums[left] == nums[left-1] && left < right);
- do{--right;} while(nums[right] == nums[right+1] && left < right);
- }
- }
- }
- return res;
- }
- };
还是没独立做出来,服了
- class Solution {
- public:
- vector<vector<int>> threeSum(vector<int>& nums) {
- // 指针 a + b + c = 0
- vector<vector<int>> result;
- sort(nums.begin(), nums.end());
- int head = 0;
- for (head = 0; head < nums.size(); ++head) {
- // 剪枝
- if (nums[head] > 0) break;
- // todo:a去重
- if (head > 0 && nums[head] == nums[head - 1]) continue;
- int left = head + 1, right = nums.size() - 1;
- // left < right
- while (left < right) {
- // todo:b去重 c去重
- if (nums[head] + nums[left] + nums[right] < 0) {
- ++left;
- while(left < right && nums[left] == nums[left - 1]) ++left;
- }
- else if (nums[head] + nums[left] + nums[right] > 0) {
- --right;
- while(left < right && nums[right] == nums[right + 1]) --right;
- }
- else {
- result.push_back({nums[head], nums[left], nums[right]});
- ++left;
- while(left < right && nums[left] == nums[left - 1]) ++left;
- --right;
- while(left < right && nums[right] == nums[right + 1]) --right;
- }
- }
- }
- return result;
- }
- };
这里对B C去重没有考虑到
- class Solution {
- public:
- vector<vector<int>> threeSum(vector<int>& nums) {
- sort(nums.begin(), nums.end());
- vector<vector<int>> res;
- for (int i = 0; i < nums.size(); i++) {
- // 剪枝
- if (nums[i] > 0) break;
- if (i > 0 && nums[i] == nums[i - 1]) continue;
-
- int left = i + 1, right = nums.size() - 1;
- while (left < right) {
- // 和 > 0,right--;和 < 0,left++
- if (nums[i] + nums[left] + nums[right] > 0) {
- right--;
- } else if (nums[i] + nums[left] + nums[right] < 0) {
- left++;
- } else {
- res.push_back({nums[i], nums[left], nums[right]});
- // b c去重
- while (right > left && nums[right] == nums[right - 1]) right--;
- while (right > left && nums[left] == nums[left + 1]) left++;
- right--;
- left++;
- }
- }
- }
- return res;
- }
- };
- class Solution {
- public:
- vector<vector<int>> threeSum(vector<int>& nums) {
- vector<vector<int>> res;
- sort(nums.begin(), nums.end());
- // i left-> <-right
- for (int i = 0; i < nums.size(); i++) {
- if (nums[i] > 0) break;
- if (i > 0 && nums[i] == nums[i - 1]) continue;
- int left = i + 1;
- int right = nums.size() - 1;
- while (left < right) {
- int sum = nums[i] + nums[left] + nums[right];
- if (sum < 0) {
- left++;
- while(left < right && nums[left] == nums[left - 1]) left++;
- } else if (sum > 0) {
- right--;
- while(left < right && nums[right] == nums[right + 1]) right--;
- } else {
- res.push_back(vector<int>{nums[i], nums[left], nums[right]});
- left++;
- while(left < right && nums[left] == nums[left - 1]) left++;
- right--;
- while(left < right && nums[right] == nums[right + 1]) right--;
- }
- }
- }
- return res;
- }
- };
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。