LeetCode_ 4 sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0. A solution set is:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)
无聊 3sum 的变形
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<vector<int>> res;
int len = num.size();
if(len < ) return res;
sort(num.begin(),num.end());
for(int i = ; i< len-;++i){
while(i> && i< len- && num[i] == num[i-])++i;
for(int j = i+; j< len-; ++j){
while(j!=i+ && j< len-&&num[j] == num[j-])++j;
int left = j+;
int right = len-;
while(left < right){
int sum = num[i] + num[j] +num[left]+num[right];
if(sum == target){
vector<int> ans;
ans.push_back(num[i]);
ans.push_back(num[j]);
ans.push_back(num[left]);
ans.push_back(num[right]);
res.push_back(ans);
left++;
while(left<right && num[left]==num[left-]) ++left;
right--;
while(left < right && num[right] == num[right+]) --right;
}else if(sum <target){
left++;
while(left<right && num[left]==num[left-]) ++left;
}else{
right--;
while(left < right && num[right] == num[right+]) --right;
}
}//while(left <right)
}//for j
}// for i
return res;
}
};
LeetCode_ 4 sum的更多相关文章
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Sum of Left Leaves 左子叶之和
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
随机推荐
- Mybatis+SpringMVC实现分页查询(附源码)
Maven+Mybatis+Spring+SpringMVC实现分页查询(附源码) 一.项目搭建 关于项目搭建,小宝鸽以前写过一篇Spirng+SpringMVC+Maven+Mybatis+MySQ ...
- gradle 集成到myeclipse
新的项目用到gradle,所以学了下,地址:http://dist.springsource.com/release/TOOLS/gradle :help 下,安装好,重启即可,gradle作为mav ...
- objective-c 加号 减号 - +
“加号代表static”是错误的说法,可能跟你那样表达的人其实意思是:“前置加号的方法相当于Java 里面的静态方法”. 在Oc中,方法分为类方法和实例方法. 前置加号(+)的方法为类方法,这类方法是 ...
- UIActionSheet底部弹出框
<底部弹出框来提示用户信息> 1.遵循代理方法<UIActionSheetDelete> 2.调用放法 [UIActionSheet *sheet=[UIActio ...
- Lucene为不同字段指定不同分词器(转)
在lucene使用过程中,如果要对同一IndexWriter中不同 Document,不同Field中使用不同的analyzer,我们该如何实现呢? 通过对<lucene in action&g ...
- hash定义
* 若结构中存在关键字和K相等的记录,则必定存储在f(K)的位置上.由此,不需比较便可直接取得所查记录.这个对应关系f称为 散列函数(Hash function),按这个思想建立的表为 散列表. * ...
- android shape的使用详解以及常用效果(渐变色、分割线、边框、半透明阴影效果等)
shape使用.渐变色.分割线.边框.半透明.半透明阴影效果. 首先简单了解一下shape中常见的属性.(详细介绍参看 api文档 ) 转载请注明:Rflyee_大飞: http://blog.cs ...
- Sniffer抓包教程
上网络信息安全的时候用了下,中途出现了一堆奇葩的事,这里就不提了... 上教程: 先把虚拟机里面的防火墙给关了,主机防火墙也关了 之前由于ip自己设置了,然后一直ping不通,后面把ip改成自动获取就 ...
- Dhroid框架笔记(IOC、EventBus)
dhroid 目前包含了6大组件供大家使用1.Ioc容器: (用过spring的都知道)视图注入,对象注入,接口注入,解决类依赖关系2.Eventbus: android平台事件总线框架,独创延时事件 ...
- memcache的安装及管理
一.Memcache概述 Memcache(内存,缓存):是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个巨大的hash表.(key=value)(是用C语言开发的,并且需要libeven ...