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. MongDB之各种修改操作

    接口IMongDaoUpdate: package com.net.test.mongdb.dao; import com.net.test.mongdb.entity.User; public in ...

  2. 虚拟机中配置SQL SERVER2008R2远程访问

    VM虚拟机中配置数据库访问 选择虚拟机设置--硬件--网络适配器,选择桥接模式:直接连接物理网络 不可选用主机模式(与主机共享专用网络) 数据库远程配置,转自:http://jingyan.baidu ...

  3. [BZOJ3524]区间问题(主席树)

    Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. ...

  4. HDOJ 2120 Ice_cream's world I

    Ice_cream's world I ice_cream's world is a rich country, it has many fertile lands. Today, the queen ...

  5. js中基础数据类型

    变量声明 undefined //未定义只声明   var age; alert(name);function fc(a1,a2,a3) { //alert(a1); //alert(a2); //a ...

  6. Python-S9-Day87——admin的使用

    01 admin的使用1 02 admin的使用2 03 admin的使用3 04 url方法的使用 05 单例模式 06 admin源码之注册功能 07 admin源码之url设计 08 admin ...

  7. stdlib.h中自带的两个算法qsort,bsearch

    http://zh.cppreference.com/w/c/algorithm ========== void qsort( void *ptr, size_t count, size_t size ...

  8. zookeeper Zookeeper

    这是ZooKeeper客户端库的主要类.使用一个ZooKeeper服务,应用程序必须首先实例化ZooKeeper类的对象.所有的迭代都将通过调用ZooKeeper类的方法来完成.除非另有说明,该类的方 ...

  9. mysql 修改密码 开启远程访问权限

    修改密码 update user set password=password('') where user='root'; FLUSH   PRIVILEGES; 远程访问权限: GRANT ALL ...

  10. ssh公钥私钥

    ssh配置文件vi /etc/ssh/sshd_config putty下载http://www.chiark.greenend.org.uk/~sgtatham/putty/download.htm ...