1. Question

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 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]

]

2. Solution

这个问题的解法和3sum一样,只不过现在是4个数求和了,先遍历所有两个数字的组合,然后在去找另外两个数,时间复杂度为O(n^3)。

3. Code

class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
if (nums.size() <= 3)
return vector<vector<int>>();
sort(nums.begin(), nums.end()); vector<vector<int>> res;
for (int i = 0; i < nums.size() - 3; i++) {
for (int j = i + 1; j < nums.size() - 2; j++) {
int start = j + 1;
int end = nums.size() - 1;
int tmp_target = target - (nums[i] + nums[j]);
while (start < end) {
int tmp = nums[start] + nums[end];
if (tmp > tmp_target)
end--;
else if (tmp < tmp_target)
start++;
else {
vector<int> result;
result.push_back(nums[i]);
result.push_back(nums[j]);
result.push_back(nums[start]);
result.push_back(nums[end]);
res.push_back(result); // 去掉start开始重复的
while (start < end && nums[start] == result[2])
start++;
// 去掉end开始重复的
while (end > start && nums[end] == result[3])
end--;
}
}
// 去掉重复的第二个数
while (j + 1 < nums.size() - 2 && nums[j + 1] == nums[j])
j++;
}
// 去掉重复的第一个数
while (i + 1 < nums.size() - 3 && nums[i + 1] == nums[i])
i++;
}
return res;
}
};

LeetCode——4Sum的更多相关文章

  1. LeetCode——4Sum &amp; 总结

    LeetCode--4Sum & 总结 前言 有人对 Leetcode 上 2Sum,3Sum,4Sum,K Sum问题作了总结: http://blog.csdn.net/nanjunxia ...

  2. [LeetCode] 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  3. [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 = tar ...

  4. leetcode — 4sum

    import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * Source : https://oj.l ...

  5. LeetCode 4Sum 4个数之和

    题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target.以vector<vector<int>>来返回,也就是二维的,列长为4 ...

  6. 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 = tar ...

  7. Leetcode: 4Sum II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  8. leetcode 4sum python

    class Solution(object): def fourSum(self, nums, target): """ :type nums: List[int] :t ...

  9. LeetCode 4Sum (Two pointers)

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

随机推荐

  1. winform实现QQ聊天气泡200行代码

    c# winform实现QQ聊天气泡界面,原理非常简单,通过webKitBrowser(第三方浏览器控件,因为自带的兼容性差)加载html代码实现,聊天界面是一个纯HTML的代码,与QQ的聊天界面可以 ...

  2. flask中db.init_app(app)讲解

    http://www.pythondoc.com/flask/extensiondev.html http://www.pythondoc.com/flask/extensiondev.html#fl ...

  3. HBase-MR

    一.需求1:对一张表的rowkey进行计数 官方HBase-Mapreduce 需求1:对一张表的rowkey进行计数 1)导入环境变量 export HBASE_HOME=/root/hd/hbas ...

  4. print文档

    文档 def print(self, *args, sep=' ', end='\n', file=None): # known special case of print ""& ...

  5. 在django中实现支付宝支付(支付宝接口调用)

    支付宝支付 正式环境:用营业执照,申请商户号,appid 测试环境:沙箱环境:https://openhome.alipay.com/platform/appDaily.htm?tab=info 支付 ...

  6. Error-The content of element type "web-app" must match "(icon?,display-

    错误描述 The content of element type "web-app" must match "(icon?,display- name?,descript ...

  7. 使用python操作文件实现购物车程序

    使用python操作文件实现购物车程序 题目要求如下: 实现思路 始终维护一张字典,该字典里保存有用户账号密码,购物车记录等信息.在程序开始的时候读进来,程序结束的时候写回文件里去.在登录注册的部分, ...

  8. django的所有app放在一个文件夹下便于管理

    1.新建一个python Package,名字叫apps 2.拖拽以后的app到apps文件夹下,把Search for references勾选去掉,重要重要重要!!!! 3.右键点击apps文件夹 ...

  9. (转)spring mvc forward与redirect

    forward 转发,如return "forward:/hello"; 浏览器的地址栏不会变,但是有视图返回来 redirect 重定向,如return "redire ...

  10. 数据库之ADO

    ADO是一种跨多种语言的数据库访问技术. 在MFC里面微软公司将这些函数封装为以下几个类. 在VS2013版本的MFC中,这些类是如下定义的. CDaoDatabase Class:https://m ...