4sum

k-sum问题都可以有2-sum引申出来解决,但是时间复杂度为O(n^k-1)。应该要用哈希解决才好的,之后再看看

class Solution{
private:
  vector<vector<int> > ans;
public:
  vector<vector<int> >fourSum(vector<int> &num, int target){
    sort(num.begin(),num.end());
    ans.clear();
    for(int i = 0; i < num.size(); ++i) {
      if(i > 0 && num[i] == num[i-1]) continue;
      for(int j = i+1; j < num.size(); ++j){
          if(j > i+1 && num[j] == num[j-1]) continue;
          int k = j+1;
          int t = num.size()-1;
          while(k < t){
            if(k > j+1 && num[k] == num[k-1]){
              k++;
              continue;
            }
            if(t < num.size()-1 && num[t] == num[t+1]){
              t--;
              continue;
            }
            int sum = num[i]+num[j]+num[k]+num[t];
            if(sum == target){
              vector<int>a;
              a.push_back(num[i]);
              a.push_back(num[j]);
              a.push_back(num[k]);
              a.push_back(num[t]);
              ans.push_back(a);
              k++;
            }
            else if(sum < target)
              k++;
            else
              t--;
          }
      }
    }
    return ans;
  }
};
时间: 2024-12-20 20:11:48

4sum的相关文章

[Leetcode] 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 order.

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 454. 4Sum II

454. 4Sum II Add to List Description Submission Solutions Total Accepted: 8398 Total Submissions: 18801 Difficulty: Medium Contributors: Samuri Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[

[LeetCode] 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 exampl

18 &amp; 454. 4Sum I &amp; II

18. 4Sum I (32lines) Given a integer array and target, find all unique quadruplets sum is target. Solution(O(N^3)): Similar as 3sum problem, add one more loop and be careful about the duplicates. 1 public class Solution { 2 public List<List<Integer&

[leetcode]4Sum @ Python

原题地址:http://oj.leetcode.com/problems/4sum/ 题意:从数组中找到4个数,使它们的和为target.要求去重,可能有多组解,需要都找出来. 解题思路:一开始想要像3Sum那样去解题,时间复杂度为O(N^3),可无论怎么写都是Time Limited Exceeded.而同样的算法使用C++是可以通过的.说明Python的执行速度比C++慢很多.还说明了一点,大概出题人的意思不是要让我们去像3Sum那样去解题,否则这道题就出的没有意义了.这里参考了kitt的解

【Leetcode】4Sum

题目链接:https://leetcode.com/problems/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 quadrupl

[LeetCode][JavaScript]4Sum

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 4Sum 4个数之和

题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target.以vector<vector<int>>来返回,也就是二维的,列长为4,有多少个序列就多少行,每行都是唯一的,且升序. 思路: 方法一:用类似3sum的方法,先确定下第1个元素,再确定第2个元素,剩下两个元素用“两个指针”.前提是已排序.这个方法主要是怎么去重,这里提供两种方法: 1)用unordered_set,只要找到一个序列就检查里面有没有这样的序列,若没有就插入,这样保

leetcode 3Sum 3Sum Closest 4Sum

这几个题很典型也是国外一些知名公司经常会问到的题 3Sum: 排序,避免重复,时间复杂度O(n^2) class Solution { public: vector<vector<int> > threeSum(vector<int> &num) { int len=num.size(); sort(num.begin(),num.begin()+len); vector<vector<int> > ret; ret.clear(); i