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: 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>& nums, int target) {
int l = nums.size(), sum, left, right, i, j;
vector<vector<int>> ans;
if (l<)
return ans;
sort(nums.begin(), nums.end());
for (i = ; i<l - ; i++)
{
if(i && nums[i] == nums[i-])
continue;
for (j = i + ; j<l - ; j++)
{
if(nums[i]+nums[j]+nums[j+]+nums[j+]>target)
break;
if(nums[i]+nums[j]+nums[l-]+nums[l-]<target)
continue;
if (j>i + && nums[j] == nums[j - ])
continue;
sum = nums[i] + nums[j];
left = j + ;
right = l - ;
while (left < right)
{
if (sum + nums[left] + nums[right] == target)
ans.push_back({ nums[i], nums[j], nums[left++], nums[right--] });
else if (sum + nums[left] + nums[right] < target)
left++;
else
right--;
while (left>j+ && nums[left] == nums[left - ])
left++;
while(right<l- && nums[right] == nums[right + ])
right--;
}
}
}
return ans;
}
};

18. 4Sum -- 找到数组中和为target的4个数的更多相关文章

  1. (回溯法)数组中和为S的N个数

    Given a list of numbers, find the number of tuples of size N that add to S. for example in the list ...

  2. 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数 例如给定nums = [2,7,11,15],target = 9

    python解决方案 nums = [1,2,3,4,5,6] #假如这是给定的数组 target = 9 #假如这是给定的目标值 num_list = [] #用来装结果的容器 def run(nu ...

  3. 【算法习题】正整数数组中和为sum的任意个数的组合数

    1.递归实现(参考:https://blog.csdn.net/hit_lk/article/details/53967627) public class Test { @org.junit.Test ...

  4. Two sum(给定一个无重复数组和目标值,查找数组中和为目标值的两个数,并输出其下标)

    示例: nums = [1,2,5,7] target = [6] return [0,2] Python解决方案1: def twoSum(nums, target): ""&q ...

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

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

  6. 1. Two Sum&&15. 3Sum&&18. 4Sum

    题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up t ...

  7. 15. 3Sum、16. 3Sum Closest和18. 4Sum

    15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...

  8. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  9. [LeetCode][Python]18: 4Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...

随机推荐

  1. ABAP Enhancement:第一部分

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  2. NSValue

    1.利用NSValue包装自定义的结构体    typedef struct{        int age;        char *name;        double height;    ...

  3. 《FLASH CC 2015 CANVAS 中文教程》——2、基本的交互(点击、触摸)事件

    注::如果你对 FLASH 这个软件操作不够熟悉,建议你可以先看看FLASH动画之类的书. :FLASH CC 在文中直接简称为CC. :以下所以文章中所说的快捷键 如果你按了不起作用,请检查是否有其 ...

  4. 删除List中制定的值的方法

    /** * * @param args */ public static void main(String[] args) { List<String> list = new ArrayL ...

  5. iOS - Swift NSDate 时间

    前言 NSDate public class NSDate : NSObject, NSCopying, NSSecureCoding NSDate 用来表示公历的 GMT 时间(格林威治时间).是独 ...

  6. iOS - UIImageView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImageView : UIView @available(iOS 2.0, *) public class U ...

  7. Redis基础知识之—— 5个必须了解的事情【★★★★★】

    一.掌控储存在Redis中的所有键 数据库的主要功能是储存数据,但是对于开发者来说,因为应用程序需求或者数据使用方法的改变,忽略存储在数据库中的某些数据是非常正常的,在Redis中同样如此.你可能忽视 ...

  8. bootstrap 固定定位

    <!DOCTYPE html> <html><head><meta charset="UTF-8"><title>Boo ...

  9. 转:HTTP 301 跳转和302跳转的区别

    301和302 Http状态有啥区别?301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于: 301 redirect: 301 代表永久性转移(Permanently ...

  10. AJAX的简介

    AJAX 指异步JavaScript及XML(Asynchronous JavaScript And XML(异步JavaScript和XML)),是指一种创建交互式网页应用的网页开发技术. 国内通常 ...