Problem:Given an array S of n integers, are there elements abc, 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) 这题其实就是之前的变种,我是这样想的,首先还是排序好,然后根据固定四个的头和尾,即i为头,从0开始到倒数第四个,j为尾巴从尾开始到i+2. 再来一个left=i+1 right=j-1 如果i++之后和前面一个相等,那就直接continue,因为已经在之前一个i算过了,同理,j--之后如果和之前的j相等的话也是不用计算的直接continue 这样的话复杂度就是n3方
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target)
{
vector<vector<int> > sum;
sum.clear();
if(num.size() < )
return sum;
sort(num.begin(), num.end()); for (int i = ; i < num.size() - ; ++i)
{
if (i - >= && num[i - ] == num[i])
continue;
for (int j = num.size() - ; j > i + ; --j)
{
if(j + < num.size() && num[j + ] == num[j])
continue;
int left = i + , right = j - ;
while(left < right)
{
if (num[i] + num[left] + num[right] + num[j] == target)
{
if(sum.size()== || sum.size()> && !(sum[sum.size() - ][]==num[i] && sum[sum.size() - ][]==num[left] && sum[sum.size() - ][]==num[right]))
{
vector<int> tep;
tep.push_back(num[i]);
tep.push_back(num[left]);
tep.push_back(num[right]);
tep.push_back(num[j]);
sum.push_back(tep);
}
left++;
right--;
}
else if (num[i] + num[left] + num[right] + num[j] < target)
left++;
else if (num[i] + num[left] + num[right] + num[j] > target)
right--;
}
}
}
return sum;
}
};

leetcode第17题--4Sum的更多相关文章

  1. LeetCode第[17]题(Java):Letter Combinations of a Phone Number

    题目:最长公共前缀 难度:EASY 题目内容: Given a string containing digits from 2-9 inclusive, return all possible let ...

  2. LeetCode 第17题--电话号码的组合(DFS)

    1. 题目 2.题目分析与思路 3.代码 1. 题目 输入:"23" 输出:["ad", "ae", "af", &qu ...

  3. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  4. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  5. Leetcode第1题:两数之和

    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的 两个 整数.你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素.示例:给定 nums ...

  6. 【JavaScript】Leetcode每日一题-青蛙过河

    [JavaScript]Leetcode每日一题-青蛙过河 [题目描述] 一只青蛙想要过河. 假定河流被等分为若干个单元格,并且在每一个单元格内都有可能放有一块石子(也有可能没有). 青蛙可以跳上石子 ...

  7. 【python】Leetcode每日一题-寻找旋转排序数组中的最小元素

    [python]Leetcode每日一题-寻找旋转排序数组中的最小元素 [题目描述] 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组nums ...

  8. 【JavaScript】【dp】Leetcode每日一题-解码方法

    [JavaScript]Leetcode每日一题-解码方法 [题目描述] 一条包含字母 A-Z 的消息通过以下映射进行了 编码 : 'A' -> 1 'B' -> 2 ... 'Z' -& ...

  9. [LeetCode每日一题]153.寻找旋转排序数组中的最小值

    [LeetCode每日一题]153.寻找旋转排序数组中的最小值 问题 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组 nums = [0,1, ...

随机推荐

  1. POJ 2996 &amp; 2993 国际象棋布局 模拟

    Description Your task is to read a picture of a chessboard position and print it in the chess notati ...

  2. Java设计模式偷跑系列(十八)建模和责任链模式的实现

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/40018231 责任链模式(ChainOfResponsibility): 有多个对象,每一 ...

  3. 记2014“蓝桥杯全国软件大赛&quot;决赛北京之行

    5月29,30日 最终到了这一天.晚上有数据结构课,10点多的火车,我们就没有去上课,下午在宿舍里收拾东西,晚上8点左右从南校出发,9点半多到达火车站和老师学长学姐们会和. 第一次去北京,第一次买的卧 ...

  4. linux 安装jdk-7u45-linux-x64.tar.gz

    1.官网下载jdk-7u45-linux-x64.tar.gz 载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-dow ...

  5. Robot Framework自动化测试(一)---第一个脚本(转)

    最近工具中用Robot Framework框架来做自动化,所以,花时间学习了一下. =======所需环境=================== Python: https://www.python. ...

  6. UIBarButtonItem 小记边

     watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuZ3poZW4xOTkwMDcwMQ==/font/5a6L5L2T/fontsize/400/ ...

  7. SSIS从理论到实战,再到应用(3)----SSIS包的变量,约束,常用容器

    原文:SSIS从理论到实战,再到应用(3)----SSIS包的变量,约束,常用容器 上期回顾: SSIS从理论到实战,再到应用(2)----SSIS包的控制流   首先我们来看看包里面的变量 SSIS ...

  8. LCS 小结

    转载链接:http://www.cnblogs.com/PJQOOO/p/3897745.html 第一步:先计算最长公共子序列的长度. 实现第一步: 设一个C[i][j]: 保存Xi与Yj的LCS的 ...

  9. javascript中对字符串的操作总结

    原文:javascript中对字符串的操作总结 没听过一句话吗?程序员的世界,不处理字符串就是处理数组.这是群里的一位前辈和我说的,显然这和我之前理解的DOM是javascript的核心的不同的,看了 ...

  10. js 实现键盘记录 兼容FireFox和IE

    这两天突然想弄弄js的键盘记录,所以就小研究了一下. 主要分四个部分 第一部分:浏览器的按键事件 第二部分:兼容浏览器 第三部分:代码实现和优化 第四部分:总结 第一部分:浏览器的按键事件 用js实现 ...