LeetCode: 4 Sum

原创 2012年03月21日 06:22:44

Given an array S of n integers, are there elements abc, 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, a ≤ b ≤ c ≤ d)
  • 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)
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace::std;
class Solution {
public:
	long RSHash(string str)
	{
		int a = 876283;
		int b = 21382;
		long hash = 0;
		for(int i = 0; i < str.size(); i++)
		{
			hash = a * hash + str[i];
			a = a * b;
		}
		return hash;
	}
    vector<vector<int> > fourSum(vector<int> &num, int target) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
		int i = 0, j = i + 1, l = num.size() - 1;
		string str;
		int key = 0;
		vector< vector<int> > result;
		vector<int> set;
		map<long, int> mymap;
		if(num.size() < 4)
		{
			return result;
		}
		sort(num.begin(), num.end());
		for(int x = 0; x < num.size() - 3; x++){
			int a = num[x];
		for(i = x + 1; i < num.size() - 2; i++)
		{
			int b = num[i];
			for(j = i + 1, l = num.size() - 1; j < l; )
			{
				int c = num[j];
				int d = num[l];
				if(a + b + c + d < target)
				{
					j++;
				}
				else if(a + b + c + d > target)
				{
					l--;
				}
				else
				{
					str.clear();
					str += a;
					str += b;
					str += c;
					str += d;
					key = RSHash(str);
					if(mymap.find(key) == mymap.end())
					{
						mymap[key] = 1;
						set.push_back(a);
						set.push_back(b);
						set.push_back(c);
						set.push_back(d);
						result.push_back(set);
						set.clear();
					}
					j++;
				}
			}
		}
		}
		return result;
        
    }
};

int main()
{
	vector<int> aa;
	
	aa.push_back(-1);
	aa.push_back(0);
	aa.push_back(1);
	aa.push_back(2);
	
	aa.push_back(-1);
	
	aa.push_back(-4);
	
	Solution ss;
	ss.fourSum(aa,4);
//	ss.print(ss.threeSum(aa));
}

Round 2:

class Solution {
public:
    vector<vector<int> > fourSum(vector<int> &num, int target) {
    
        vector<vector<int> > result;
        if(num.size() < 4)
            return result;
        std::sort(num.begin(), num.end());
        for(int i = 0; i < num.size()-3; i++)
        {
            for(int j = i+1; j < num.size()-2; j++)
            {
                int l = j+1, r = num.size()-1;
                while(l < r)
                {
                    if(num[i] + num[j] + num[l] + num[r] == target)
                    {
                        vector<int> cur;
                        cur.push_back(num[i]);
                        cur.push_back(num[j]);
                        cur.push_back(num[l]);
                        cur.push_back(num[r]);
                        result.push_back(cur);
                        while(l+1 < r && num[l] == num[l+1])
                            l++;
                        while(r-1 > l && num[r] == num[r-1])
                            r--;
                        l++;
                        r--;
                    }
                    else if(num[i] + num[j] + num[l] + num[r] < target)
                    {
                        l++;
                    }
                    else
                        r--;
                }
                while(j+1 < num.size()-2 && num[j] == num[j+1])
                    j++;
            }
            while(i+1 < num.size()-3 && num[i] == num[i+1])
                i++;
        }
        return result;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode 18 4Sum

问题https://leetcode.com/problems/4sum/解法一(138ms)枚举第一个和第四个数,第二个和第三个数tow pointer 复杂度O(n3n^3)class Solu...

leetcode第一刷_4Sum

用暴力水过了两个,正在沾沾自喜,当看到连续加的规模增加到4时,眼泪直接掉下来了,新想这下我这水货原形毕露了,没想到。。 我抱着试试看的心态,又写了一种水水的暴力解,每次固定前面的两个节点,后面的两个...

[LeetCode]018-4-Sum

题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...

LeetCode——4Sum & 总结

LeetCode——4Sum & 总结 有人对 Leetcode 上 2Sum,3Sum,4Sum,K Sum问题作了总结: http://blog.csdn.net/nanjunxiao/arti...

LeetCode算法题——18. 4Sum

题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a&...

[leetcode]4Sum

4Sum Difficulty:mediun Given an array S of n integers, are there elements a, b, c, and d in S...

[Leetcode]4Sum

Given an array S of n integers, are there elements a, b, c, and d in S such that a +&...

leetcode 第18题 4Sum

这个题和之前的题挺相似的,我首先想到的是直接用上以前的方法,先确定一个数(用循环实现),然后用3Sum的方法做,其实每种方法都大同小异啦~ bool cmp(const int&a, const in...

leetcode——4Sum

题目: Given an array S of n integers, are there elements a, b, c, and d in S such that ...

《LeetBook》leetcode题解(18) : 4Sum[M]

018. 4Sum 问题Given an array S of n integers, are there elements a, b, c, and d in S such that a + b ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)