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

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

  1. class Solution {
  2. public:
  3. vector<vector<int> > fourSum(vector<int> &num, int target) {
  4. vector<vector<int> > ans;
  5. if(num.size()<)
  6. return ans;
  7.  
  8. sort(num.begin(),num.end());
  9. for(int i = ;i<num.size()-;i++)
  10. {
  11. if(i> && num[i]==num[i-])
  12. continue;
  13.  
  14. calcThree(num,target - num[i],i,ans);
  15. }
  16. return ans;
  17. }
  18. void calcThree(vector<int> &num,int threeSum,int firstPosition,vector<vector<int> > &ans)
  19. {
  20. int k = ;
  21.  
  22. for(int i = firstPosition+;i<num.size()-;i++)
  23. {
  24. if(i>firstPosition+ && num[i] == num[i-])
  25. continue;
  26. int j = i+;
  27.  
  28. k = num.size()-;
  29.  
  30. while(j<k)
  31. {
  32. if(num[i]+num[j]+num[k] == threeSum)
  33. {
  34. if(ans.size()== || ans.size()> && !(num[firstPosition] == ans[ans.size()-][] && num[i]==ans[ans.size()-][]&& num[j] ==ans[ans.size()-][] ))
  35. {
  36. vector<int> ansPiece;
  37. ansPiece.push_back(num[firstPosition]);
  38. ansPiece.push_back(num[i]);
  39. ansPiece.push_back(num[j]);
  40. ansPiece.push_back(num[k]);
  41. ans.push_back(ansPiece);
  42. }
  43. j++;
  44. }
  45. else if(num[i]+num[j]+num[k] < threeSum)
  46. j++;
  47. else
  48. k--;
  49. }
  50. }
  51. }
  52. };

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. Python操作SQLLite(基本操作)

    SQLite 是一个软件库,实现了自给自足的.无服务器的.零配置的.事务性的 SQL 数据库引擎.SQLite 是在世界上最广泛部署的 SQL 数据库引擎.SQLite 源代码不受版权限制. Pyth ...

  2. 百度之星初赛A 今夕何夕

    今夕何夕 今天是2017年8月6日,农历闰六月十五. 小度独自凭栏,望着一轮圆月,发出了"今夕何夕,见此良人"的寂寞感慨. 为了排遣郁结,它决定思考一个数学问题:接下来最近的哪一年 ...

  3. Appium环境搭建及“fn must be a function”问题解决

    由于appium在线安装比较困难,大多数应该是由于FQ造成的吧,索性直接下载appium安装包:http://pan.baidu.com/s/1bpfrvjD nodejs下载也很缓慢,现提供node ...

  4. input框中的必填项之取消当前input框为必填项

    html5新增了一个required属性,可以使用这个属性对文本框设置必填项,直接在input文本框上添加required即可 . 效果如图:   

  5. document.domain跨子域

    document.domain 用来得到当前网页的域名.比如在地址栏里输入: javascript:alert(document.domain); //www.315ta.com 我们也可以给docu ...

  6. IOS开发学习笔记005-数组

    数组 数组故名思议就是一组数据的集合. int a[10];//可以存储10个整数 char  c[8];//可以存储8个字符‘ 一般格式:数组类型 数组名[元素个数]: 数组元素的访问:下标,a[2 ...

  7. log4j2用Log4jContextSelector启动参数配置全局异步日志是如何使用disruptor

    与 log4j2用asyncRoot配置异步日志是如何使用disruptor差异有几个: 给disruptor实例的EventFactory不同 此处EventFactory采用的是RingBuffe ...

  8. Leetcode 502.IPO

    IPO 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最多 ...

  9. linux基础(基本命令)

    Linux学习                         1.Linux安装.配置 Linux的操作背景介绍 Linux操作系统 开源.自由且开发源代码的类Unix操作系统 厂商较多 著名的有R ...

  10. re.search 与 re.match的区别

    search ⇒ find something anywhere in the string and return a match object. match ⇒ find something at ...