18. 4Sum

Posted 小河沟大河沟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了18. 4Sum相关的知识,希望对你有一定的参考价值。

欢迎fork and star:Nowcoder-Repository-github

18. 4Sum

题目

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]
]

解析

class Solution_18 {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {

        vector<vector<int>> vecs;
        
        if (nums.size()<4)
        {
            return vecs;
        }
        sort(nums.begin(), nums.end());
        for (int first = 0; first < nums.size() - 3;first++)
        {
            if (first>0&&nums[first]==nums[first-1])
            {
                continue;
            }

            for (int seconde = first + 1; seconde < nums.size() - 2;seconde++)
            {
                if (seconde>first+1&&nums[seconde]==nums[seconde-1])
                {
                    continue;
                }

                int third = seconde + 1;
                int fouth = nums.size() - 1;

                while (third<fouth)
                {
                    vector<int> temp = {nums[first],nums[seconde],nums[third],nums[fouth]};
                    int sum = accumulate(temp.begin(), temp.end(), 0);
                    if (sum==target)
                    {
                        vecs.push_back(temp);
                        third++; fouth--;  //细节问题
                        while (third < fouth&&nums[third] == nums[third - 1])
                        {
                            third++;
                        }
                        while (third < fouth&&nums[fouth] == nums[fouth + 1])
                        {
                            fouth--;
                        }
                        // third++; fouth--;放在后面有bug

                        //// start++; end--放在后面就是和后面的数比较
                        //while (start < end&&nums[start] == nums[start + 1])
                        //{
                        //  start++;
                        //}
                        //while (start < end&&nums[end] == nums[end - 1])
                        //{
                        //  end--;
                        //}
                        //start++; end--;

                        ////或者用do() while()循环
                        //do{
                        //  k++;
                        //} while (k < l && ivec[k] == ivec[k - 1]);
                        //do{
                        //  l--;
                        //} while (k < l && ivec[l] == ivec[l + 1]);
                    }
                    else if(sum<target)
                    {
                        third++;
                    }
                    else
                    {
                        fouth--;
                    }
                }
            }
        }
        return vecs;
    }
};

题目来源

以上是关于18. 4Sum的主要内容,如果未能解决你的问题,请参考以下文章

18. 4Sum

leetcode 18 4Sum

LeetCode——18. 4Sum

18. 4Sum

LeetCode - 18. 4Sum

java 18. 4Sum(#)。java