题目意思:给一个乱序数组,在里面寻找三个数之和为target的所有情况,这些情况不能重复,增序排列

思路:采用3Sum的做法

   ps:有见一种用hash的,存任意两个元素的和,然后变成3sum问题,需要判断重复

   图书馆的网,已经到了令人发指的程度,我告诫自己千万不要暴躁。

 class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> ans;
vector<int> vec();
int k,l,temp;
sort(nums.begin(),nums.end());
for(int i=;i<nums.size();++i){
if(i>&&nums[i]==nums[i-])continue;
for(int j=i+;j<nums.size();++j){
if(j>i+&&nums[j]==nums[j-])continue;
k=j+;
l=nums.size()-;
while(k<l){
if(k>j+&&nums[k]==nums[k-]){
++k;
continue;
}
temp=nums[i]+nums[j]+nums[k]+nums[l];
if(temp>target)--l;
else if(temp<target)++k;
else{
vec[]=nums[i];
vec[]=nums[j];
vec[]=nums[k];
vec[]=nums[l];
ans.push_back(vec);
--l;
++k;
}
}
}
}
return ans;
}
};

18 4Sum(寻找四个数之和为指定数的集合Medium)的更多相关文章

  1. 15 3Sum(寻找三个数之和为指定数的集合Medium)

    题目意思:给一个乱序数组,在里面寻找三个数之和为0的所有情况,这些情况不能重复,增序排列 思路:前面2sum,我用的是map,自然那道题map比双指针效率高,这道题需要先排序,再给三个指针,i.j.k ...

  2. LeetCode 18 4Sum (4个数字之和等于target)

    题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...

  3. lintcode:四个数之和

    题目 四数之和 给一个包含n个数的整数数组S,在S中找到所有使得和为给定整数target的四元组(a, b, c, d). 样例 例如,对于给定的整数数组S=. 满足要求的四元组集合为: (-1, 0 ...

  4. 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 ...

  5. 18. 4Sum[M]四数之和

    题目 Given an array nums of n integers and an integer target, are there elements a, b, c and d in nums ...

  6. LeetCode:18. 4Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/4sum/description/ 2. 题目要求 给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Unity 的 unitypackage 的存放路径

    Windows,C:\Users\<username>\AppData\Roaming\Unity\Asset Store Mac OS X,~/Library/Unity/Asset S ...

  2. css背景图片位置:background的position

    position的两个参数:水平方向的位置,垂直方向的位置----------该位置是指背景图片相对于前景对象的 1.background:url(../image/header.jpg) no-re ...

  3. 天津Uber优步司机奖励政策(1月18日~1月24日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. Pipe - POJ 1039(线段相交交点)

    题目大意:有一个不反光并且不透光的管道,现在有一束光线从最左端进入,问能达到的最右端是多少,输出x坐标.   分析:刚开始做是直接枚举两个点然后和管道进行相交查询,不过这样做需要考虑的太多,细节不容易 ...

  5. poj 3687 Labeling Balls【反向拓扑】

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12246   Accepted: 3508 D ...

  6. Android color(颜色) 在XML文件和java代码中

    Android color(颜色) 在XML文件和java代码中,有需要的朋友可以参考下. 1.使用Color类的常量,如: int color = Color.BLUE;//创建一个蓝色 是使用An ...

  7. 利用NPOI开源的读写Excel、WORD等微软OLE2组件读写execl,控制样式或单元格

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  8. 使用dom4j解析xml文件,并封装为javabean对象

    dom4j是一个java的XML api,性能优异.功能强大.易于使用.这里使用dom4j对xml文件进行解析,并完成对文件的封装. 实现对xml文件的解析,主要使用到的是dom4j中的SAXRead ...

  9. android 50 进程优先级

    程序在磁盘叫程序,程序加载到内存运行起来叫进程,优先级5个级别,内存不足的时候会杀掉低级别进程. Active Process:最上面用户可以操作的. Visible Process:可见进程,部分可 ...

  10. navigation的pushViewController卡顿问题

    问题:在ios中一个viewController中,添加下面代码: <pre name="code" class="objc">UIViewCont ...