Leetcode 4Sum
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) {
vector<vector<int> > res;
int n = num.size();
if( n < ) return res;
sort(num.begin(),num.end());
for(int i = ; i < n-; ++ i){
if(i != && num[i]== num[i-]) continue;
for(int j = i+; j < n-; ++ j){
if(j!=i+ && num[j] == num[j-] ) continue;
int start = j+, end = n-;
while(start < end){
int sum = num[i]+num[j]+num[start]+num[end];
if(sum > target) end--;
else if(sum < target) start++;
else{
vector<int> a;
a.push_back(num[i]);a.push_back(num[j]);
a.push_back(num[start]);a.push_back(num[end]);
res.push_back(a);
do{start++;}while(start < end && num[start] == num[start-]);
do{end--;}while(start < end && num[end] == num[end+]);
}
}
}
}
return res;
}
};
Leetcode 4Sum的更多相关文章
- LeetCode——4Sum & 总结
LeetCode--4Sum & 总结 前言 有人对 Leetcode 上 2Sum,3Sum,4Sum,K Sum问题作了总结: http://blog.csdn.net/nanjunxia ...
- [LeetCode] 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- [LeetCode] 4Sum 四数之和
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- leetcode — 4sum
import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * Source : https://oj.l ...
- LeetCode 4Sum 4个数之和
题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target.以vector<vector<int>>来返回,也就是二维的,列长为4 ...
- Leetcode: 4Sum II
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- leetcode 4sum python
class Solution(object): def fourSum(self, nums, target): """ :type nums: List[int] :t ...
- LeetCode 4Sum (Two pointers)
题意 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- LeetCode——4Sum
1. Question Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + ...
随机推荐
- C++ GC
http://www.codeproject.com/Articles/912/A-garbage-collection-framework-for-C http://www.codeproject. ...
- PHP ajax基础
后台对数据处理,并返回前台: <?php //接收 $action = $_GET['action']; if ($action == 'orderID') { $orderID = trim( ...
- ThinkPhp 验证码不显示图片
验证码不显示原因分析: 1.先确保是否开启了 gd库 2.页面是否存在bom头 3.导入的ORG.Util.Image是否存在 4.入口文件中是否有define('APP_DEBUG', TRUE); ...
- rails 常用的知识点
按惯例先上网址: http://guides.ruby-china.org/ 适合初学者很好的文章 ===========================知识点================ ...
- Bash 中同名的内部命令和外部命令
昨天有个人在 bug-bash 上问:为什么 [ --help 没有输出帮助信息.有人回答他了,原因是 coreutils 提供的 [ 命令才接受 --help 选项,Bash 自己的 [ 命令不接受 ...
- vertx核心类之VertxImpl
在Vert.x中,Vertx接口是最为重要的一个接口,vertx-core的基础功能都在此接口中提供.这篇文章中我们就来分析一下Vertx接口体系的内部实现以及创建流程.本文对应Vert.x的版本为 ...
- Github.com的Git和TortoiseGit图文教程
图文介绍Windows系统下使用 Github账户 + msysgit + TortoiseGit 进行文件管理的方法. 安装 安装mysysgit 下载地址:msysgit 安装过程: 0.启动 1 ...
- Daily Scrum Meeting ——TenthDay
一.Daily Scrum Meeting照片 二.Burndown Chart 新增了几个issues 三.项目进展 1.完成了登录界面与管理员和发布者界面的整合. 2.活动发布者界面的完成 四.问 ...
- C和指针 第六章 数组名与指针
指针的算术运算符是指针和数组之间的一种关联,但不是唯一关联: 可以使用数组名作为指向数组第一个元素的指针,但是不可以给数组名赋新的值. //如下声明a int a[10]; //用a作为指向数组第一个 ...
- 帝国CMS视频
http://list.youku.com/albumlist/show?id=17602333&ascending=1.html