18. 4Sum

195 篇文章 0 订阅
问题描述

Given an array nums of n integers and an integer target, are there elements a, b, c, 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]
]

题目链接:


思路分析

从数组中找到所有能够用四个数组成target的元素。

理解了4sum,3sum和2sum也就不在话下了。首先是对数组进行排序,然后看一下最小值情况和最大值情况,接着开始循环,注意避免重复,然后计算边界情况,太小了就继续,太大了就break。如果来到了boundary,看有没有4个一样的,没有就break,这些条件都不满足的话,就调用3sum。

对于3sum,一样的,计算最小值最大值情况,然后进入循环,注意循环的开始是在low和结束条件high - 1。然后一样的重复、太小、太大和boundary的情况。接着调用2sum。

2sum就是用两个指针从头尾开始遍历,一样要先判断最大值最小值,节省计算量,然后开始遍历,如果得到了结果,那么要记得存储之后,忽略重复情况。然后太小就left+,太大就right-。一样的写法。

代码
class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int>> res;
        int l = nums.size();
        if(l < 4)
            return res;
        sort(nums.begin(), nums.end());
        int max = nums[l - 1];
        if(4 * nums[0] > target || 4 * max < target)
            return res;
        int num;
        for(int i = 0; i < l; i++){
            num = nums[i];
            if(i > 0 && num == nums[i - 1])//avoid duplicate
                continue;
            if(num + 3 * max < target)//num is too small
                continue;
            if(4 * num > target)//num is too big
                break;
            if(4 * num == target){//num is boundary, test Special case and we can stop.
                if(i + 3 < l && nums[i + 3] == num)// special case
                    res.push_back(vector<int>(4, num));
                break;
            }
            threeSum(nums, target - num, res, i + 1, l - 1, num);
        }
        return res;
    }

    void threeSum(vector<int>& nums, int target, vector<vector<int>>& res, int low, int high, int first){
        if (low + 1 >= high)
            return;
        int max = nums[high];
        if(3 * nums[low] > target || 3 * max < target)
            return;
        int num;
        for(int i = low; i < high - 1; i++){
            num = nums[i];
            if(i > low && num == nums[i - 1])//avoid duplicate
                continue;
            if(num + 2 * max < target)//num is too small
                continue;
            if(3 * num > target)//num is too big
                break;
            if(3 * num == target){//num is boundary, test SC and we can stop.
                if(i + 1 < high && nums[i + 2] == num)// special case
                    res.push_back({first, num, num, num});
                break;
            }
            twoSum(nums, target - num, res, i + 1, high, first, num);
        }

    }

    void twoSum(vector<int>& nums, int target, vector<vector<int>>& res, int low, int high, int first, int second){
        if (low >= high)
            return;
        int max = nums[high];
        if(2 * nums[low] > target || 2 * max < target)
            return;
        int left = low, right = high, sum;
        while(left < right){
            sum = nums[left] + nums[right];
            if (sum == target){
                res.push_back({first, second, nums[left], nums[right]});

                int t = nums[left];
                while(++left < right && nums[left] == t ){}

                t = nums[right];
                while(left < --right && nums[right] == t){}
            }
            if(sum < target)
                left++;
            if(sum > target)
                right--;
        }
        return;
    }

};

时间复杂度: O(n3) O ( n 3 )
空间复杂度: O(n) O ( n )


反思

这么写法是为了节省计算量,其实开始直接两个for循环嵌套加里面的2sum写法来做,代码量会小一些。

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int>> res;
        sort(nums.begin(), nums.end());
        int length = nums.size();
        if(nums.size() < 4 || nums[0] * 4 > target || nums[length - 1] * 4 < target)
            return res;
        for(int i = 0; i < length; i++){
            for(int j = i + 1; j < length - 1; j++){
                int left = j + 1, right = length - 1;
                while(left < right){
                    int sum = nums[i] + nums[j] + nums[left] + nums[right];
                    if(sum < target)
                        left++;
                    else if(sum > target)
                        right--;
                    else{
                        res.push_back({nums[i], nums[j], nums[left],nums[right]});
                        while(left < right && nums[left] == nums[left + 1]){left++;}
                        while(left < right && nums[right] == nums[right - 1]){right--;}
                        left++;
                        right--;
                    }
                }
                while(j < length - 1 && nums[j] == nums[j + 1]){j++;}
            }
            while(i < length && nums[i] == nums[i + 1]){i++;}
        }
        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值