leetcode : 4Sum

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)

和2Sum,3Sum的思路基本上一样,这类问题都是先排序,然后由K Sum转换成K-1 Sum,

比如4Sum先选一个,然后从剩下的数字中求3Sum

所以问题最后还是时间复杂度的估算问题,K Sum的暴力求解是O(n ^ K)的复杂度,据说有证明最好的优化为n(n ^ k -1)所以放心大胆的一遍遍搜。。

AC代码:

class Solution {
public:
    vector<vector<int> > fourSum(vector<int> &num, int target) {
        sort(num.begin(), num.end());
        vector<vector<int>> ans;
        if(num.size() < 4){
            return ans;
        }
        for(int i = 0; i < num.size() - 3; ++i){
            if(i == 0 || num[i] != num[i - 1]){
                for(int j = i + 1; j < num.size() - 2; ++j){
                    if(j == i + 1 || num[j] != num[j - 1]){
                        int left = j + 1;
                        int right = num.size() - 1;
                        int CTarget = target - num[i] - num[j];
                        while(left < right){
                            if(num[left] + num[right] < CTarget){
                                while(left < right && num[left] == num[(left++) + 1]){ //去除重复元素,并且保证left至少会+1,除非left >= right
                                    ;
                                }
                            }else if(num[left] + num[right] > CTarget){
                                while(left < right && num[right] == num[(right--) - 1]){
                                    ;
                                }
                            }else{
                                vector<int> temp = {num[i], num[j], num[left], num[right]};
                                ans.push_back(temp);
                                while(left < right && num[right] == num[(right--) - 1]){
                                    ;
                                }
                                while(left < right && num[left] == num[(left++) + 1]){
                                    ;
                                }
                            }
                        }
                    }
                }
            }
        }

        return ans;
    }
};
时间: 2024-10-23 14:18:44

leetcode : 4Sum的相关文章

[LeetCode] 018. 4Sum (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 018.4Sum (Medium) 链接: 题目:https://oj.leetcode.com/problems/4sum/ 代码(github):https://github.com/illuz/leetcode 题意: 给一个数列 S ,找出四个数 a,b,c,d 使得a + b + c + d = targ

[LeetCode] 454. 4Sum II 四数之和II

Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the r

LeetCode——18. 4Sum

一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数的和等于target,找出所有的四元组,当然这些四元组不能有重复的. 三.题解: 这道题实质就是3sum的变形,关于4sum问题,已经在https://www.cnblogs.com/wangkundentisy/p/9079622.html这里说过了,无外乎最外面两层循环,最里面的循环使用哈希表或

LeetCode 017 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: Elements in a quadruplet (a,b,c,d) must be in non-descending o

LeetCode:Longest Palindromic Substring

Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 动态规划解法 T(n) = O(n^2)  ,S(n) = O(n^2); Solutio

leetcode:Pascal&amp;#39;s Triangle

一.     题目 经典题目,杨辉三角,输入行数.生成杨辉三角的数组. 二.     分析 首先,我们知道有例如以下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数的和值 须要注意的是,当行数为0时输出[[1]] 结果为一个二维数组,所以不难想到解决方式. 每层保存前一行的指针,然后当前行数据依据上一行来得到,每一个元素就是上一行两个相邻元素相加(第一个和最后一个元素是1). 算法时间复杂度应该是O(1+2+3+...+n)=O(n^2),空间上仅仅须要二维数

LeetCode: Triangle 题解

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 11 (i

LeetCode: Permutations II 题解

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].题解:依旧使用的是DFS的思想. 首先需要遍历输入数组,获取一共有多少种不同的数字,每个数字有多少个. 最简单的方法,

LeetCode:(Array-189) Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s