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

【Leetcode】4sum

时间:2014-08-20 14:06:32      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:des   blog   io   strong   for   ar   art   div   

 

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:

  • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, abcd)
  • 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)

 

枚举两个元素,后面两个采取2sum的策略双指针逼近。

 

1st (9 tries)

class Solution 
{
public:
    vector<vector<int> > fourSum(vector<int> &num, int target) 
    {
        //时间复杂度为n的立方
        //算法,先定位前2个,然后后两个利用2 sum的思路
        //去重复的思路可能不是很好
        vector<vector<int> > re;
        vector<int> tmpnum;
        if(num.size() < 4)
            return re;
        sort(num.begin(),num.end());
        for(int i = 0;i < num.size() - 3;i++)
        {
            if(i > 0 && num[i] == num[i - 1])
                continue;
            for(int j = i + 1;j < num.size() - 2;j++)
            {
                if(j > i+ 1 && num[j] == num[j - 1])
                    continue;
                tmpnum.clear();
                tmpnum.push_back(num[i]);
                tmpnum.push_back(num[j]);
                int tmptarget = target - num[i] - num[j];
                
                int start = j+1;
                int end = num.size() - 1;
                
                while(start < end)
                {
                    if(num[start] + num[end] == tmptarget)
                    {
                        if(start > j+1 && end < num.size() - 1 && num[start] == num[start - 1] && num[end] == num[end + 1])
                        {
                            start++;
                            end--;
                            continue;
                        }
                        tmpnum.push_back(num[start]);
                        tmpnum.push_back(num[end]);
                        re.push_back(tmpnum);
                        tmpnum.pop_back();
                        tmpnum.pop_back();
                        start++;
                        end--;
                    }
                    else if(num[start] + num[end] > tmptarget)
                    {
                        end--;
                    }
                    else
                    {
                        start++;
                    }
                }
            }
        }
        return re;
    }
};

  

2nd ( 3 tries)

class Solution {
public:
    vector<vector<int> > ans;
    vector<int> res;
    vector<vector<int> > fourSum(vector<int> &num, int target) {
        int n = num.size();
        sort(num.begin(),num.end());
        int _1,_2,_3,_4;
        for(_1 = 0;_1 < n-3;_1++) {
            if(_1 > 0 && num[_1] == num[_1-1])
                continue;
            for(_2 = _1 + 1;_2 < n-2;_2++) {
                if(_2 > _1 + 1 && num[_2] == num[_2-1])
                    continue;
                _3 = _2 + 1;
                _4 = n - 1;
                while(_3 < _4) {
                    if(_3 > _2 + 1 && _4 < n-1 && num[_3] == num[_3-1] && num[_4] == num[_4+1]) {
                        _3++;
                        _4--;
                        continue;
                    }
                    if(num[_1] + num[_2] + num[_3] + num[_4] == target) {
                        res.push_back(num[_1]);
                        res.push_back(num[_2]);
                        res.push_back(num[_3]);
                        res.push_back(num[_4]);
                        ans.push_back(res);
                        res.clear();
                        _3++;
                        _4--;
                    }
                    else if(num[_1] + num[_2] + num[_3] + num[_4] > target) {
                        _4--;
                    }
                    else {
                        _3++;
                    }
                }
            }
        }
        return ans;
    }
};

  

【Leetcode】4sum,布布扣,bubuko.com

【Leetcode】4sum

标签:des   blog   io   strong   for   ar   art   div   

原文地址:http://www.cnblogs.com/weixliu/p/3924349.html

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