题目意思:给一个乱序数组,在里面寻找三个数之和为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. poj 2484 A Funny Game(博弈)

    A Funny Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4639   Accepted: 2855 Desc ...

  2. JavaScript 兼容处理IE67之 !"a"[0]

    IE67对字符串进行取值需要使用charAt()方法,不能直接通过数组方式的坐标访问: <!DOCTYPE html> <html> <head> <meta ...

  3. [LeetCode] Distinct Subsequences 解题思路

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  4. xapian倒排索引的归并流程

    Xapian的检索流程和大部分搜索系统都一样,就先从倒排表抽取候选文档,然后结合其他信息进行排序,取top文档作为搜索结果,具体流程如下: 图1 xapian搜索流程 具体流程 在terms中找到do ...

  5. FastDfs 说明、安装、配置

    fastdfs是一个开源的,高性能的的分布式文件系统,他主要的功能包括:文件存储,同步和访问,设计基于高可用和负载均衡,fastfd非常适用于基于文件服务的站点,例如图片分享和视频分享网站 fastf ...

  6. Erasing Edges - SGU 136(构造多边形)

    题目大意:已知一个多边形上的每条边的中点,还原出来一个多边形. 分析:因为偶数是不固定的,所以可以为任意起点,奇数只有一个,可以所有中点加减算出来第一个点,然后就是简单的向量计算点的位置了...... ...

  7. [转]jquery中使用event.target的几点

    转自: http://www.cnblogs.com/12go/archive/2011/12/28/2304502.html 1.this和event.target的区别: js中事件是会冒泡的,所 ...

  8. 【设计模式 - 5】之适配器模式(Adapter)

    1      模式简介 适配器模式解决的问题:让原本因为接口不兼容而不能一起工作的类可以一起工作. 适配器模式的UML原理图如下图所示: 从上图可见,客户想要用Target接口实现Adaptee接口中 ...

  9. 提高你30%的设计效率的PPT快捷键

    在编辑幻灯片的状态下: [Ctrl]+[A]选择全部对象或幻灯片 [Ctrl]+[B]应用(解除)文本加粗 [Ctrl]+[C]复制 [Ctrl]+[D]快速复制对象 [Ctrl]+[E]段落居中对齐 ...

  10. ※C++随笔※=>☆C++基础☆=>※№→C++中 #include<>与#include""

    #include<> 使用尖括号表示在包含文件目录中去查找(包含目录是由用户在设置环境时设置的),而不在源文件目录去查找: #include"" 使用双引号则表示首先在 ...