LeetCode OJ--4Sum *
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 *的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- 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 ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- Python操作SQLLite(基本操作)
SQLite 是一个软件库,实现了自给自足的.无服务器的.零配置的.事务性的 SQL 数据库引擎.SQLite 是在世界上最广泛部署的 SQL 数据库引擎.SQLite 源代码不受版权限制. Pyth ...
- 百度之星初赛A 今夕何夕
今夕何夕 今天是2017年8月6日,农历闰六月十五. 小度独自凭栏,望着一轮圆月,发出了"今夕何夕,见此良人"的寂寞感慨. 为了排遣郁结,它决定思考一个数学问题:接下来最近的哪一年 ...
- Appium环境搭建及“fn must be a function”问题解决
由于appium在线安装比较困难,大多数应该是由于FQ造成的吧,索性直接下载appium安装包:http://pan.baidu.com/s/1bpfrvjD nodejs下载也很缓慢,现提供node ...
- input框中的必填项之取消当前input框为必填项
html5新增了一个required属性,可以使用这个属性对文本框设置必填项,直接在input文本框上添加required即可 . 效果如图:
- document.domain跨子域
document.domain 用来得到当前网页的域名.比如在地址栏里输入: javascript:alert(document.domain); //www.315ta.com 我们也可以给docu ...
- IOS开发学习笔记005-数组
数组 数组故名思议就是一组数据的集合. int a[10];//可以存储10个整数 char c[8];//可以存储8个字符‘ 一般格式:数组类型 数组名[元素个数]: 数组元素的访问:下标,a[2 ...
- log4j2用Log4jContextSelector启动参数配置全局异步日志是如何使用disruptor
与 log4j2用asyncRoot配置异步日志是如何使用disruptor差异有几个: 给disruptor实例的EventFactory不同 此处EventFactory采用的是RingBuffe ...
- Leetcode 502.IPO
IPO 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最多 ...
- linux基础(基本命令)
Linux学习 1.Linux安装.配置 Linux的操作背景介绍 Linux操作系统 开源.自由且开发源代码的类Unix操作系统 厂商较多 著名的有R ...
- re.search 与 re.match的区别
search ⇒ find something anywhere in the string and return a match object. match ⇒ find something at ...