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. 获得span里面价值(在特殊情况下 )

    如何获得A? <div class="warpper"> <span class="content"> A <span>12 ...

  2. C++ Primer 学习笔记_43_STL实践与分析(17)--再谈迭代器【中】

    STL实践与分析 --再谈迭代器[中] 二.iostream迭代[续] 3.ostream_iterator对象和ostream_iterator对象的使用 能够使用ostream_iterator对 ...

  3. 使用Advanced Installer 自动部署 Arcgis Engine Runtime 10.0

    原文:使用Advanced Installer 自动部署 Arcgis Engine Runtime 10.0 目前采用Arcgis9.2 + c#(vs2008)作为程序开发平台,是一个不错的搭配. ...

  4. 如何关闭CBox(2.4版本号)强制升级的形式

    从今天开始2.4.0.9版本号CBox,提示检测到新的版本号,能够使用后必须更新为新版本号,提示表见下面的例子. 此次升级是强制升级.假如你选择不升级(单击窗体上的升级提示右下角"辍学but ...

  5. Python3.2官方文件翻译-工具列表和十进制浮点计算

    8.7 列表工具 许多数据结构需要通过内置列表类型来满足.但,有时候在不同的性能取舍需要选择一个实现. Array模块能提供一个像列表的array对象,它只能存储同类数据而且更加简洁. 接下来样例展示 ...

  6. Ansible@一个有效的配置管理工具--Ansible configure management--翻译(四)

    不要未经书面许可转载 第三章是长,因为,我会分几个部分来翻译. Advanced Playbooks So far the playbooks that we have looked at are s ...

  7. Android JNI环境要SQLite加密模块简介

    免费的SQLite开源源代码仅仅给提供了两个函数,仅仅有实现这两个函数才干实现数据库总体加密. 然后废了点劲从网上找了一个已经实现好的开源库http://sourceforge.net/project ...

  8. JAVA类型修饰符(public,protected,private,friendly)

    JAVA类型修饰符(public,protected,private,friendly) public的类.类属变量及方法.包内及包外的不论什么类均能够訪问:protected的类.类属变量及方法,包 ...

  9. JSP具体条款——response对象

    response对象 response为响应对象client要求.输出信息到客户.他封装JSP反应生成.发送client在回应client要求. 1.重定向网页 使用response对象的sendRe ...

  10. linux_cp_远程copy

    1:远程copy  [linux对linux 远程拷贝]   scp 文件名  root@远程ip:/路径/    将本地home目录下的test.tar的文件拷贝到远程主机192.168.1.23的 ...