题目描述:4Sum

Given an array S of n integers, are there elements abc, 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)

代码如下:

class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
// Note: The Solution object is instantiated only once.
vector<vector<int>> res;
int numlen = num.size(); if(num.size()<4)
return res; sort(num.begin(),num.end()); set<vector<int>> tmpres; for(int i = 0; i < numlen; i++){
for(int j = i + 1; j < numlen; j++){ int begin = j+1;
int end = numlen-1; while(begin < end){
int sum = num[i]+ num[j] + num[begin] + num[end];
if(sum == target){
vector<int> tmp;
tmp.push_back(num[i]);
tmp.push_back(num[j]);
tmp.push_back(num[begin]);
tmp.push_back(num[end]);
tmpres.insert(tmp);
begin++;
end--;
}
else if(sum<target) begin++;
else end--;
}
}
} set<vector<int>>::iterator it = tmpres.begin();
for(; it != tmpres.end(); it++)
res.push_back(*it);
return res;
}
};

LeetCode 018 4Sum的更多相关文章

  1. 【JAVA、C++】LeetCode 018 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 ...

  2. [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 ...

  3. 【LeetCode】018 4Sum

    题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  4. LeetCode——18. 4Sum

    一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...

  5. LeetCode 18 4Sum (4个数字之和等于target)

    题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...

  6. [LeetCode] 18. 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 ...

  7. 018 4Sum 四个数的和

    给定一个含有 n 个整数的数组 S,数列 S 中是否存在元素 a,b,c 和 d 使 a + b + c + d = target ?请在数组中找出所有满足各元素相加等于特定值的不重复组合.注意:解决 ...

  8. [LeetCode] 454. 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  9. [LeetCode] 18. 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 ...

随机推荐

  1. Java学习的第五十五天

    1.例11.1继承学生类 import java.util.Scanner; import java.util.*; public class Cjava { public static void m ...

  2. Java学习的第十天

    1.类方法 实例方法 自定义方法 2.今天使用visio不太会使用,方法覆盖不懂. 3.明天将方法剩余部分学完

  3. 分四个阶段学习python并找到一份好工作

    第一阶段 关注公众号"轻松学编程"了解更多. 详细学习资料 需要时间一个月. 1.python概念 ​ python是一种解释型.面向对象.动态数据类型的高级程序语言. ​ 理解: ...

  4. JavaWeb项目问题记录

    模板 [遇到的问题] [时间] [原因] [解决方案] [排查思路及方式] 思路: 1) 2) [遇到的问题] 品优购项目中运营商页面查询广告信息是,无法正常查询,错误如下: Failed to lo ...

  5. MIT 6.S081 Lab5 Copy-On-Write Fork

    前言 最近绝大多数的空闲时间都拿来锤15-445了,很久没动6.S081.前几天回头看了一下一个月前锤完的Lazy Allocation,自己写的代码几乎都不认识了.......看来总结之类的东西最好 ...

  6. js给多级复杂动态变量赋值

    1 function SetVal(field, val) { 2 var arr = field.split("."); 3 var str = arr[0]; 4 if (wi ...

  7. php 检测敏感字

    public function getMin($content){//调用接口 $content_url ="http://www.ju1.cn/index.php/Index/add.ht ...

  8. HotSpot的启动过程(配视频进行源码分析)

    本文将详细介绍HotSpot的启动过程,启动过程涉及到的逻辑比较复杂,细节也比较多,为了让大家更快的了解这部分知识,我录制了对应的视频放到了B站上,大家可以参考. 第4节-HotSpot的启动过程 下 ...

  9. appium-appium的等待时间

    #三种appium设置等待时间的方法 #第一种 sleep(): 设置固定休眠时间. python 的 time 包提供了休眠方法 sleep() , 导入 time包后就可以使用 sleep()进行 ...

  10. scala的异常处理try catch

    object Test { def main(args: Array[String]) { try { val f = new FileReader("input.txt") } ...