题面

Given an array nums of n integers and an integer target, are there elements abc, and d in nums such that a + bc + d = target? Find all unique quadruplets in the array which gives the sum of target.

给定无序数组,找到和为target的不重复长度为4的子序列

样例

1. Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
2. Given array nums = [0, 0, 0, 0], and target = 0.
solution set is:
[
[0, 0, 0, 0]
]
note: This example need to think specially!
3. Given array nums = [-3, -3, -3, 2, 2, 2,0, 0, 0, 3, 3, 3], and target = 0.
solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
Note: How to deal with the duplicate quadruplets ?

思路(这里是4个数的和为target,我们之前不是做过3个数的吗?Refer this one 正好可以用上)

1. 升序排序(sort即可)

2. 不可避免地要遍历数组(i)

3. 借鉴leetcode-15中的三个数的和,我们如法炮制,搜索剩下的三个数(j = i+1, l=j+1, r = size()-1);对参数有疑惑的话,看下面代码。

时间复杂度:O(n3),暴力的话应该会超时,没试!

这题里面尤其要注意避免重复

源码

 class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> res;
int len = nums.size();
if(len < )
return res; sort(nums.begin(), nums.end());
for(int i=; i<=len-; i++)
{
while(i> && i<=len- && nums[i]==nums[i-])
i++;//消除i的重复
for(int j=i+; j<=len-; j++)
{
while(j>i+ && j<=len- && nums[j]==nums[j-])
j++;//消除j的重复
int l=j+, r=len-;
while(l < r)
{
int sum = nums[i] + nums[j] + nums[l] + nums[r];
if(sum > target)
r--;
else if(sum < target)
l++;
else
{
res.push_back(vector<int> {nums[i], nums[j], nums[l], nums[r]});
while(l<r && nums[l]==nums[l+]) l++;//消除l的重复
while(l<r && nums[r]==nums[r-]) r--;//消除r的重复
l++; r--;
}
}
}
}
return res;
}
};

如果对消除重复有疑问的童鞋,请留言, 或者自行把example 3 手推一遍就明白了。

Array + two points leetcode.18 - 4Sum的更多相关文章

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

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

  3. 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 ...

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

  8. C#解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 ...

  9. [leetcode]18. 4Sum四数之和

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

随机推荐

  1. osg create shape

    osg::ref_ptr<osg::Node> OSG_Qt_::createSimple() { osg::ref_ptr<osg::Geode> geode = new o ...

  2. java最简单复制文件方法,不依赖任何框架

    java最简单复制文件方法   把java2.txt内容复制到java.txt中 import java.io.File; import java.io.IOException; import jav ...

  3. Qt编写控件属性设计器1-加载插件

    一.前言 加载插件是整个属性设计器的第一步要打通的功能,插件中的控件都加载不了,后面就别搞别玩下去了没法玩的,要从一个动态库中加载出来控件,肯定需要用到反射机制,以前做.NET开发的时候就觉得反射这个 ...

  4. 【Leetcode_easy】645. Set Mismatch

    problem 645. Set Mismatch 题意: solution1: 统计每个数字出现的次数了,然后再遍历次数数组,如果某个数字出现了两次就是重复数,如果出现了0次,就是缺失数. 注意:如 ...

  5. Python3之多重继承

    继承是面向对象编程的一个重要的方式,因为通过继承,子类就可以扩展父类的功能. Animal类层次的设计,假设我们要实现以下4中动物 Dog-狗狗 Bat-蝙蝠 Parrot-鹦鹉 Ostrich-鸵鸟 ...

  6. iOS-UIScrollView+UIPageControl简单实现

    #import "MJViewController.h"#import "RootViewController.h" @interface MJViewCont ...

  7. .Net中委托的协变和逆变详解

    关于协变和逆变要从面向对象继承说起.继承关系是指子类和父类之间的关系:子类从父类继承所以子类的实例也就是父类的实例.比如说Animal是父类,Dog是从Animal继承的子类:如果一个对象的类型是Do ...

  8. java中实现在线人数统计

    //java 代码public class SessionCounter implements HttpSessionListener { private static int activeSessi ...

  9. S12. Android 检查更新功能实现

    [概述] 不需要从 App Store 或者指定官网直接下载,可以通过 App 直接更新到最新版本. [流程设计] 显示当前版本信息以及版本更新日志 提供 “检查更新” 按钮,点击事件处理逻辑: 1) ...

  10. [转帖]彻底弄懂UTF-8、Unicode、宽字符、locale

    彻底弄懂UTF-8.Unicode.宽字符.locale linux后端开发   已关注   彻底弄懂UTF-.Unicode.宽字符.locale unicode 是字符集 utf-8是编码格式.. ...