[LeetCode 解题报告]018.4 Sum

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/caicaiatnbu/article/details/82534622

Description:

Given an array nums of n integers and an integer target, are there elements abc, and d in nums such that a + b + c + d =target? Find all unique quadruplets in the array which gives the sum of target.

Note:

The solution set must not contain duplicate quadruplets.

Example:

Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]

思路:这类题统称K-Sum,思路都特别相似;

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        
        vector<vector<int> > res;
        int n = nums.size();
        
        if (n < 4)
            return res;
        
        sort(nums.begin(), nums.end());
        
        for(int i = 0; i < n-3; i ++) {
            
            if(i > 0 && nums[i] == nums[i-1])
                continue;
            
            if(nums[i] + nums[i+1] + nums[i+2] + nums[i+3] > target)
                break;
            
            if(nums[i] + nums[n-3] + nums[n-2] + nums[n-1] < target)
                continue;
            
            for(int j = i+1; j < n-2; j ++) {
                if(j > i+1 && nums[j] == nums[j-1])
                    continue;
                
                if(nums[i] + nums[j] + nums[j+1] + nums[j+2] > target)
                    break;
                
                if(nums[i] + nums[j] + nums[n-2] + nums[n-1] < target)
                    continue;
                
                int l = j+1, r = n-1;
                
                while(l < r) {
                    
                    int tempSum = nums[i] + nums[j] + nums[l] + nums[r];
                    
                    if(tempSum < target)
                        l ++;
                    else if(tempSum > target)
                        r --;
                    else {
                        res.push_back(vector<int> {nums[i], nums[j], nums[l], nums[r]});
                        do {
                            l ++;
                        } while(nums[l] == nums[l-1] && l < r);
                        
                        do {
                            r --;
                        } while(nums[r] == nums[r+1] && l < r);
                    }
                }
                
            }
            
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/caicaiatnbu/article/details/82534622