【4Sum】cpp

题目

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)

代码

class Solution {
public:
    vector<vector<int> > fourSum(vector<int> &num, int target)
    {
            vector<vector<int> > result;
            sort(num.begin(), num.end());
            unsigned int len = num.size();
            if (len<4) return result;
            for (int i = 0; i < len-3; ++i)
            {
                if ( i>0 && num[i]==num[i-1] ) continue;
                for (int j = len-1; j>i+2; --j)
                {
                    if ( j<len-1 && num[j]==num[j+1] ) continue;
                    int k = i+1;
                    int z = j-1;
                    while(k<z)
                    {
                        const int tmp_sum = num[i]+num[j]+num[k]+num[z];
                        if (tmp_sum==target)
                        {
                            vector<int> tmp;
                            tmp.push_back(num[i]);
                            tmp.push_back(num[k]);
                            tmp.push_back(num[z]);
                            tmp.push_back(num[j]);
                            result.push_back(tmp);
                            ++k;
                            while ( num[k]==num[k-1] && k<z ) ++k;
                            --z;
                            while ( num[z]==num[z+1] && k<z ) --z;
                        }
                        else if (tmp_sum>target)
                        {
                            --z;
                            while ( num[z]==num[z+1] && k<z ) --z;
                        }
                        else
                        {
                            ++k;
                            while ( num[k]==num[k-1] && k<z ) ++k;
                        }
                    }
                }
            }
            return result;
    }
};

Tips:

1. 上面的代码时间复杂度O(n³)并不是最优的,网上有一些其他的可能做到O(n²)用hashmap的方式。

2. 上面的代码沿用了3Sum一样的思想:

  a. 3Sum需要固定一个方向的变量,头尾各设定一个指针,往中间逼近。

   b. 4Sum由于多了一个变量,则需要固定头并且固定尾,在内部的头尾各设定一个指针,再往中间逼近。

3. TwoSum 3Sum 4Sum这个系列到此为止了 套路基本就是固定头或尾的变量 再往中间逼

时间: 2024-10-10 08:48:33

【4Sum】cpp的相关文章

【Combinations】cpp

题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 代码: class Solution { public: vector<vector<int>> combine

【Anagrams】 cpp

题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 代码: class Solution { public: vector<string> anagrams(vector<string>& strs) { vector<string> ret; map<string,vec

【Triangle 】cpp

题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 1

【N-Queens】cpp

题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration

【Permutations】cpp

题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 代码: class Solution { public: vector<vector<int>> permute(vector&

【Subsets】cpp

题目: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If nums = [1,2,3], a solution is: [ [3], [1], [2], [

【3Sum】cpp

题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The s

【Leetcode】【4Sum】【四数之和】【C++】

题目:给定一个整数数组nums和一个目标值target,判断是否存在四个数a,b,c,d,使得a+b+c+d=target?找出所有满足条件且不重复的四元组 示例: nums = [1, 0, -1, 0, -2, 2],和 target = 0 满足要求的四元组集合为: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ] 说明: 注意找出的四元组不重合,即没有相同的四元组:且四元组按从小到大的顺序输出. 思路1:对nums数组排序,然后两个for循

【转】linux configure报错configure: error: C++ preprocessor “/lib/cpp” fails sanity 的解决办法

/lib/cpp fails sanity check的解决 在某些软件的时候,运行./configure 会报错,错误提示为: configure: error: C++ preprocessor “/lib/cpp” fails sanity  check See `config.log’ for more details 解决办法:出现该情况是由于c++编译器的相关package没有安装,以root用户登陆,在终端上执行: # yum install glibc-headers # yum