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 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)
【题意】
给定一个数组,从中取4个数使它们的和等于target, 找出全部的组合。
不能有反复;
且组合中各个数升序排列;
【思路】
思路和3Sum的思路全然同样,先确定第一个数,剩下的就又变成3Sum问题了
【代码】
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
vector<vector<int> >result;
int size=num.size();
if(size<4)return result;
sort(num.begin(), num.end()); //排序
for(int p1=0; p1<size-3; p1++){
if(p1!=0&&num[p1]==num[p1-1])continue; //第一个数排重
for(int p2=p1+1; p2<size-2; p2++){
if(p2!=p1+1 && num[p2]==num[p2-1])continue; //第二个数排重
int p3=p2+1;
int p4=size-1;
while(p3<p4){
if(p3!=p2+1 && num[p3]==num[p3-1]){p3++;continue;} //第三个数排重
int sum=num[p1]+num[p2]+num[p3]+num[p4];
if(sum==target){
vector<int> quadruplet;
quadruplet.push_back(num[p1]);
quadruplet.push_back(num[p2]);
quadruplet.push_back(num[p3]);
quadruplet.push_back(num[p4]);
result.push_back(quadruplet);
p3++;p4--;
}
else if(sum>target)p4--;
else p3++;
}
}
}
return result;
}
};
LeetCode 017 4Sum的更多相关文章
- [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 t ...
- LeetCode——18. 4Sum
一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...
- LeetCode 18 4Sum (4个数字之和等于target)
题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...
- [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 = tar ...
- [LeetCode] 454. 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] 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 = tar ...
- leetcode 日记 4sum java
整体思路同之前的一样,依然采取降低维度的方式进行 public List<List<Integer>> solution(int nums[], int target) { L ...
- 【leetcode】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 ...
- 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 = tar ...
随机推荐
- MySQL OCP
http://www.royalwzy.com/ http://www.aixchina.net/home/space.php?uid=898169
- ios开发小结之app发布升级
在近两个月的开发中,遇到了挺多问题的,几天加班加点,最后还是在年前发布并更新了一个版本,欢迎下载无觅下载. 最头疼的问题是提交app审核,之前的工程不太规范,导致一些文件icon没有设置好,直接val ...
- dedecms 调用父栏目下的所有子栏目
效果如下: 代码如下: <div class="productxilie"> <ul> {dede:channelartlist row=6 typeid ...
- 【好】Paxos以及分布式一致性的学习
Paxos,一言以蔽之,我们需要一种提交协议来确保分布式系统中的全局操作即使是在发生故障的情况下也能保证正确性. 跟拜占庭将军问题是不同的问题,虽然拜占庭也是Lamport提出的.拜占庭里面有叛徒,有 ...
- 将App发布到WasLiberty的较稳妥方法
1.将应用解压放到一个目录 具体步骤: 1.1 建立目录,假设应用包为app.war且和新建目录sp在同一目录下 #mkdir sp 1.2 将app.war 改名为app.zip,这是为了解压#mv ...
- CNN卷积神经网络新想法
近期一直在看卷积神经网络,想改进改进弄出点新东西来.看了好多论文,写了一篇综述.对深度学习中卷积神经网络有了一些新认识,和大家分享下. 事实上卷积神经网络并非一项新兴的算法.早在上世纪八十年代就已经被 ...
- Shell脚本值:运算符
算术运算符 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用. expr 是一款表达式计算工具,使用它能完成表达式的求值操作. 例如:实现两个 ...
- SAS连接MYSQL的步骤及引用数据表
1.建立逻辑库 libname dz ’物理路径'; 2.逻辑库做为桥梁连接SAS与MYSQL libname dz MYSQL USER=***** PASSWORD=**** DATABA ...
- html中图片上传预览的实现
本地图片预览 第一种方法 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type& ...
- Axure教程:滑动进度条、圆形进度环的复杂交互效果实现方法
滑动条.进度条.进度环,是产品原型中比较常见的进度展示功能.今天笔者分享的是使用Axure原型工具实现两种进度展示功能中相对复杂的交互效果. 效果一.可拖动.可显示进度值.可计算多个页面均值的滑动进度 ...