码迷,mamicode.com
首页 > 其他好文 > 详细

18. 4Sum

时间:2018-01-21 16:22:35      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:while   tin   fork   问题   cinder   temp   ==   log   repos   

欢迎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

标签:while   tin   fork   问题   cinder   temp   ==   log   repos   

原文地址:https://www.cnblogs.com/ranjiewen/p/8324190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!