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. 虚拟网卡 TUN/TAP 驱动程序设计原理

    简介 虚拟网卡Tun/tap驱动是一个开源项目,支持很多的类UNIX平台,OpenVPN和Vtun都是基于它实现隧道包封装.本文将介绍tun/tap驱动的使用并分析虚拟网卡tun/tap驱动程序在li ...

  2. Windows中压缩版的MySQL的安装、配置

    本次笔记是根据mysql-8.0.13-winx64版本编写: 1.将下载的压缩包解压到自己想放的目录 2.右键计算机 -> 属性 -> 高级系统设置 -> 环境变量 -> 系 ...

  3. c 浮点数

    一.二进制小数 十进制小数: 12.3410 == 1 * 101 + 2 * 100 + 3 * 10-1 + 4 * 10-2 = 12(34/100) (可能很多人还不知道怎么计算一个数的负幂, ...

  4. python基础-第七篇-7.3反射

    定义 反射是根据字符串的形式去对操作其成员 了解反射前,我先看看内置方法__import__,还记得这个内置方法吗? __import__  用于以字符串的形式导入模块 inp = input('请输 ...

  5. Mysql数据库常用操作语句大全

    零.用户管理: 1.新建用户: >CREATE USER name IDENTIFIED BY 'ssapdrow'; 2.更改密码: >SET PASSWORD FOR name=PAS ...

  6. Python的幂运算

    直接用例子说明

  7. lastIndexOf is not a function

    最近在开发的时候遇到了这个问题lastIndexOf is not a function,细心调试发现我传递进去的参数不是字符串类型,而且object类型,导致出现这种错误.把参数修改成字符串传递进去 ...

  8. jstl c:choose>、<c:when>和<c:otherwise>标签的简单使用介绍

    <c:choose>.<c:when>和<c:otherwise>在一起连用,可以实现Java语言中的if-else语句的功能.例如以下代码根据username请求 ...

  9. zookeeper curator客户端之增删改查

    zookeeper curator客户端之增删改查 zookeeper安装:https://www.cnblogs.com/zwcry/p/10272506.html curator客户端是Apach ...

  10. linux下多线程之pthread_detach(pthread_self())

    写个碰到的问题,记录下自己的技术之路点滴pthread_detach(pthread_self())linux线程执行和windows不同,pthread有两种状态joinable状态和unjoina ...