题面

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. 阶段5 3.微服务项目【学成在线】_day17 用户认证 Zuul_05-用户认证-认证服务查询数据库-调用查询用户接口

    用户认证服务调用根据账号查询用户的信息 怎么远程调用呢?要创建一个客户端,这个客户端其实就是一个接口 标明服务的名称是ucenter服务 这是ucenter服务里面 复制过来接口的定义,GetMapp ...

  2. PAT 甲级 1058 A+B in Hogwarts (20 分) (简单题)

    1058 A+B in Hogwarts (20 分)   If you are a fan of Harry Potter, you would know the world of magic ha ...

  3. PPT做交互效果

    1 做交互效果(点击一个按钮,弹出某个框或者跳转到某个页面)其实就是若干个PPT页面,利用给按钮增加超链接或者动作 达到点击一个按钮 跳转到另外一个PPT的效果. 2 选择一个形状组件(或者其他组件) ...

  4. LeetCode_38. Count and Say

    38. Count and Say Easy The count-and-say sequence is the sequence of integers with the first five te ...

  5. Golang 项目 GOPATH 总结

    查看GOPATH go env 项目里执行:go  get  github/winyh/XXX 命令时, 包会下载到 GOPATH第一个目录下的src文件夹 项目里引入依赖的时候会自动到GOPATH里 ...

  6. 【C#设计模式3】工厂方法模式

    一.引言 在简单工厂模式中讲到简单工厂模式的缺点,有一点是——简单工厂模式系统难以扩展,一旦添加新产品就不得不修改简单工厂方法,这样就会造成简单工厂的实现逻辑过于复杂,然而本专题介绍的工厂方法模式可以 ...

  7. 物联网防火墙himqtt源码之MQTT协议分析

    物联网防火墙himqtt源码之MQTT协议分析 himqtt是首款完整源码的高性能MQTT物联网防火墙 - MQTT Application FireWall,C语言编写,采用epoll模式支持数十万 ...

  8. vue-cli2和vue-cli3同时存在

    https://blog.csdn.net/Jioho_chen/article/details/90455778 win下 vue-cli2 和 vue-cli3 并存,一起使用开局一张图,内容慢慢 ...

  9. go json解析Marshal和Unmarshal

    Decoder: package main import ( "encoding/json" "fmt" "io" "log&qu ...

  10. K8S从入门到放弃系列-(6)kubernetes集群之kube-controller-manager部署

    摘要: 1.Kubernetes控制器管理器是一个守护进程它通过apiserver监视集群的共享状态,并进行更改以尝试将当前状态移向所需状态. 2.kube-controller-manager是有状 ...