https://oj.leetcode.com/problems/4sum/

在一个数列中,找出所有的4个数,它们的和是target.

class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
vector<vector<int> > ans;
if(num.size()<)
return ans; sort(num.begin(),num.end());
for(int i = ;i<num.size()-;i++)
{
if(i> && num[i]==num[i-])
continue; calcThree(num,target - num[i],i,ans);
}
return ans;
}
void calcThree(vector<int> &num,int threeSum,int firstPosition,vector<vector<int> > &ans)
{
int k = ; for(int i = firstPosition+;i<num.size()-;i++)
{
if(i>firstPosition+ && num[i] == num[i-])
continue;
int j = i+; k = num.size()-; while(j<k)
{
if(num[i]+num[j]+num[k] == threeSum)
{
if(ans.size()== || ans.size()> && !(num[firstPosition] == ans[ans.size()-][] && num[i]==ans[ans.size()-][]&& num[j] ==ans[ans.size()-][] ))
{
vector<int> ansPiece;
ansPiece.push_back(num[firstPosition]);
ansPiece.push_back(num[i]);
ansPiece.push_back(num[j]);
ansPiece.push_back(num[k]);
ans.push_back(ansPiece);
}
j++;
}
else if(num[i]+num[j]+num[k] < threeSum)
j++;
else
k--;
}
}
}
};

LeetCode OJ--4Sum *的更多相关文章

  1. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  2. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  3. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  4. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  5. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  7. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  8. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

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

随机推荐

  1. GBK UTF8 GB2132

    GBK就是在保存你的帖子的时候,一个汉字占用两个字节,外国人看会出现乱码,为此我中华为自己汉字编码而形成之解决方案. UTF8就是在保存你的帖子的时候,一个汉字占用3个字节.但是外国人看的话不会乱码. ...

  2. MongDB之各种删除操作

    接口IMongDaoDelete: package com.net.test.mongdb.dao; public interface IMongDaoDelete { public void del ...

  3. 常用模块之 os,json,shelve,xml模块

    os 即操作系统 在 os 中提供了很多关于文件,文件夹,路径处理的函数 这是我们学习的重点 os.path 是os模块下专门用于处理路径相关的 python是一门跨平台语言,由于每个平台路径规则不同 ...

  4. 大数运算:HDU-1042-N!(附N!位数的计算)

    解题心得: 这里使用了10000进制.很明显,因为是n!所以单个最大的数是10000*10000,使用万进制. 可以借鉴高精度的加法,单个乘了之后在进位. 很坑的一点,0!=1,数学不好WA了三次,尴 ...

  5. [Hdu3507]Print Article(斜率优化)

    Description 题意:给N个数,按顺序全部取走,每次取一段连续的区间,代价为\((S[i]-S[j])^2+M\) 其中M为一个给定的常数,\(S[i]\)为前缀和 \(N\leq 50000 ...

  6. PAT basic 1086

    1086 就不告诉你 (15 分) 做作业的时候,邻座的小盆友问你:“五乘以七等于多少?”你应该不失礼貌地围笑着告诉他:“五十三.”本题就要求你,对任何一对给定的正整数,倒着输出它们的乘积. 输入格式 ...

  7. luogu1829 [国家集训队]Crash的数字表格

    被 bs 了姿势水平--好好学习数学QAQQAQQAQ ref #include <iostream> #include <cstring> #include <cstd ...

  8. Hyper-v Server 2012 R2增强会话模式

    从Windows Server 2012 R2开始,通过使用Hyper-v增强会话模式Hyper-v中的虚拟机允许重定向虚拟机连接会话中的本地资源.这是因为Windows Server 2012 及早 ...

  9. Python+Selenium练习篇之12-获取浏览器的版本号

    本文介绍,如何通过webdriver方法获取浏览器的版本号.看起来这个功能很鸡肋,不管怎么说,还是学习下,特别是在发送自动化测试报告的时候,还是可以通过这个方法来告诉别人,执行过的脚本是通过什么浏览器 ...

  10. c++ stl在acm的入门及使用

    stl的全称为Standard Template Library,即为标准模板库,它主要依赖于模板,而不是对象,所以你需要对这个模板进行实例化,选择你要使用的类型.我们用的都是一些简单的容器吧 这里可 ...