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 + ...
随机推荐
- 海思h264解码库
海思的dll,解码h264 解码后转出yuv12 dll自己百度下载 hi_h264dec.dll hi_h264dec_w.dll 调用方法: if (H264Dec.Hi264DecA ...
- Android 签名证书
Android APK的数字签名的作用和意义 http://blog.csdn.net/gaomatrix/article/details/6568191 http://jingyan.baidu.c ...
- Backbone 学习笔记
Backbone 是一款基于模型-视图-控制器 MVC 模式的轻量级javascript 框架 ,可以用来帮助开发人员创建单页Web应用. 借助Backbone 我们可以使用REST的方式来最小化客户 ...
- 跟随 Web 标准探究DOM -- Node 与 Element 的遍历
写在前面 这篇没有什么 WebKit 代码的分析,因为……没啥好分析的,在实现里无非就是树的(先序DFS)遍历而已,囧哈哈哈……在WebCore/dom/Node.h , WebCore/dom/Co ...
- JavaScript常用技术总结!~~
//如果当前窗口不是最外层窗口,把最外层窗口链接改成当前窗口 if (window != top) top.location.href = location.href; //value值移入消失 $( ...
- golang笔记——array
1.定义一个 array 数组长度也是类型的一部分,比如长度为3的int数组与长度为5的int数组,并不是同一类型. package main import ( "strconv" ...
- Linux启动过程详解(inittab、rc.sysinit、rcX.d、rc.local)
启动第一步--加载BIOS 当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬 ...
- java笔记--关于线程同步(7种同步方式)
关于线程同步(7种方式) --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3897440.html"谢谢-- 为何要使用同步? ...
- jquery.SuperSlide.js只需要调用一个插件就能实现网页大部分特效--推荐
很棒的一个插件,http://www.superslide2.com/
- java程序打包成jar
1. 建立文件夹:proj,在该文件夹下建立3个子文件夹:lib,src 2. 在lib文件夹中放置依赖的jar包 3. 在src中放置类文件:com.cnjava.demo.Main.java 4. ...