18. 4Sum

作者: Al73r | 来源:发表于2017-09-25 20:56 被阅读0次

题目

Given an array S of n integers, are there elements a, b, c, and d in S 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.

For example, given array S = [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]
]

分析

之前的3Sum的题已经分析过kSum这一类型了,同样的解法。

实现一

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int>> ans;
        if(nums.size()<4) return ans;
        sort(nums.begin(), nums.end());
        auto first=nums.begin(), last=nums.end();
        for(auto i=nums.begin(); i<last-3; ++i){
            if(i!=first) while(i<last-3 && *i==*(i-1)) ++i;
            for(auto j=i+1; j<last-2; ++j){
                if(j!=i+1) while(j<last-2 && *j==*(j-1)) ++j;
                auto start=j+1, end=last-1;
                while(start<end){
                    int sum=*i+*j+*start+*end;
                    if(sum==target){
                        ans.push_back({*i,*j,*start,*end});
                        do ++start;
                        while(start<last && *start==*(start-1));
                        do --end;
                        while(end>=first && *end==*(end+1));
                    }
                    else if(sum<target){
                        do ++start;
                        while(start<last && *start==*(start-1));
                    }
                    else{
                        do --end;
                        while(end>=first && *end==*(end+1));
                    }
                }
            }
        }
        return ans;
    }
};

思考一

这次使用了迭代器,然而更慢???莫非是我算法的问题。
看了别人的解法,发现可以做剪枝:1)当小的这些数都大于目标数时,可结束循环;2)当外侧循环的数字与最大的那些数字相加仍然小于目标数时,可以跳过。另外,去重的部分可以集中放在写入答案的代码块中,在外侧循环中的去重不需要用while写,用if就够了。

实现二

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 - 1] + nums[n - 2] + nums[n - 3] < target) continue;
            int sum3 = target - nums[i];
            for(int j = i + 1; j < n - 2; j++) {
                if(j > i + 1 && nums[j] == nums[j - 1]) continue;
                if(nums[j] + nums[j + 1] + nums[j + 2] > sum3) break;
                if(nums[j] + nums[n - 1] + nums[n - 2] < sum3) continue;
                int front = j + 1, back = n - 1;
                int sum2 = sum3 - nums[j];
                while(front < back) {
                    int tmp = nums[front] + nums[back];
                    if(tmp < sum2) front++;
                    else if(tmp > sum2) back--;
                    else {
                        vector<int> v = {nums[i], nums[j], nums[front], nums[back]};
                        res.push_back(v);
                        while(front < back && nums[front] == v[2]) front++;
                        while(front < back && nums[back] == v[3]) back--;
                    }
                }
            }
        }
        return res;
    }
};

思考二

在看了题解之后,发现还有几种解法。其中用hashmap的解法可以达到O(n^2)的复杂度,有点惊讶。
恶补了下hashmap的知识。
http://blog.csdn.net/q_l_s/article/details/52416583
思路我基本理解了,就是先将这些数两两的和存储起来,然后遍历这些和,通过map快速找到匹配的另外的和,从而在O(n^2)的时间内完成搜索。然而,我不理解它是如何处理两个和中包含同一个元素的情况的,虽然确实过了。另外,这种方法其实所花的时间很长,所以我就不贴出来了。可能是数据量太少,体现不出优势?

相关文章

  • LeetCode #18 2018-07-28

    18. 4Sum Given an array nums of n integers and an integer...

  • 力扣每日一题:18.四数之和

    18.四数之和 https://leetcode-cn.com/problems/4sum/[https://le...

  • 18.四数之和

    18.四数之和 题目链接:https://leetcode-cn.com/problems/4sum/[https...

  • leetcode 18. 4Sum

    leetcode 18. 4Sum 题目,但是其解题思路可以延伸到 ksum 参考:1 ksum

  • 18. 4Sum

    Given an array nums of n integers and an integer target, ...

  • 18. 4Sum

    Given an array nums of n integers and an integer target, ...

  • 18. 4Sum

    Description Given an array S of n integers, are there ele...

  • 18. 4Sum

    在3Sum基础上,固定第一个数对剩下的数进行3Sum计算,复杂度为O(n^3)

  • 18. 4Sum

    题目描述:给定一个有n个整数的数组S和目标值target,找到其中所有由四个数a、b、c、d组成,使得a + b ...

  • 18. 4Sum

    题目 Given an array S of n integers, are there elements a, ...

网友评论

      本文标题:18. 4Sum

      本文链接:https://www.haomeiwen.com/subject/fbftextx.html