LeetCode——4Sum
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的更多相关文章
- LeetCode——4Sum & 总结
LeetCode--4Sum & 总结 前言 有人对 Leetcode 上 2Sum,3Sum,4Sum,K Sum问题作了总结: http://blog.csdn.net/nanjunxia ...
- [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 ...
- [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 ...
- leetcode — 4sum
import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * Source : https://oj.l ...
- LeetCode 4Sum 4个数之和
题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target.以vector<vector<int>>来返回,也就是二维的,列长为4 ...
- 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 ...
- 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 ...
- leetcode 4sum python
class Solution(object): def fourSum(self, nums, target): """ :type nums: List[int] :t ...
- 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 = ...
随机推荐
- golang的指针和切片
首先为什么要讲go的指针和切片放在一起? 因为go指针和切片都是引用类型 引用类型就是说切片和指针保存的只是内存的地址,而不是具体的值,效率在大数据读取方面效率会高很多. 1.怎么定义一个切片 方法1 ...
- Power Strings----poj2406(kmp扩展 循环节)
题目链接:http://poj.org/problem?id=2406 题意:就是求串s能够最多由多少个相同的串a串联而成: 例如 ababab 由3个ab串联而成: abababa 只能由1个aba ...
- weblogic中eclipse远程调试
1. weblogic 配置文件修改 修改文件: weblogic/weblogic103/user_projects/domains/xxxx/bin/setDomainEnv.sh(windows ...
- html5 live stream
一.传统的安防监控/流媒体音视频直播基本架构 A/V device 信号采集(yuv/rgb) ---> 转码(h264/265) ---> 网络推送(rtsp/rtmp/http/onv ...
- Java 语言中 Enum 类型的使用介绍【转载】
简介:本文主要介绍了 Java 语言中枚举类型,以及如何定制 Enum 类型的定义,如何正确使用 Enum 类型. From:http://www.ibm.com/developerworks/cn/ ...
- Mozilla Network Security Services拒绝服务漏洞
解决办法: 运行 yum update nss yum update nss
- The Cheap KD 10 is my best shoe yet
10 years of anything is fairly huge Cheap KD 10, but adding something as great as Flyknit causes it ...
- Ajax 报错 500 (Internal Server Error)
==========error======={"readyState":4,"responseText":"<html><head& ...
- Spring-2-A Magic Grid(SPOJ AMR11A)解题报告及测试数据
Magic Grid Time Limit:336MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description Tha ...
- 粗略介绍Java AQS的实现原理
本文转自 http://www.importnew.com/24006.html 感谢作者 对我很有帮助 ①引言 AQS是JDK1.5提供的一个基于FIFO等待队列一个同步器的基础框架,java中的同 ...