Array + two points leetcode.18 - 4Sum
题面
Given an array nums
of n integers and an integer target
, are there elements a, b, c, and d in nums
such that a + b+ c + 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的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- LeetCode——18. 4Sum
一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...
- LeetCode 18 4Sum (4个数字之和等于target)
题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- iOS 越狱后OpenSSH安装报错
转载自:https://www.jianshu.com/p/75f6d0f54d61 安装OpenSSH报错 尝试重启手机重新安装 将手机语言设置为英文状态 将手机设置为飞行模式,用wifi下载(我的 ...
- 【设计思路】Booking优化
https://www.uisdc.com/booking-redesign https://medium.muz.li/booking-com-ux-case-study-7ffb39e54791
- Linux——定时任务crontab
linux内置的cron进程能帮我们实现这些需求,cron搭配shell脚本,非常复杂的指令也没有问题. cron介绍 我们经常使用的是crontab命令是cron table的简写,它是cron的配 ...
- tp5.1 where 时间查询
$where_time = []; if ($_GET['s_time'] && !isset($_GET['e_time'])){ $where_time = ['add_time' ...
- jsplumb实现流程图
流程图使用工具汇总 jsPlumb,开源软件,推荐使用,参考学习链接: jsplumb学习笔记.基本概念.中文简易教程 jTopo myflow Go.js JointJS,属于商业软件 mxGrap ...
- iOS-UITextField的使用
UITextField UITextField * accountField = [[UITextField alloc] initWithFrame:CGRectMake(85.0f, 60.0f ...
- pytorch中F.softmax(x1,dim = -1) dim 取值测试及验证
# -*- coding: utf-8 -*- """ Created on Mon May 27 11:09:52 2019 @author: jiangshan &q ...
- Pytest执行用例报Hint: make sure your test modules/packages have valid Python names.
近日,使用Pytest+Appium 实现APP端UI自动化,遇到Pytest收集用例失败的情况. 报错信息如下: test_room.py:None (test_room.py) ImportErr ...
- IDEA使用 maven 搭建 SSM 框架
文章目录 pom 文件的编写 项目结构 SSM 配置文件的编写 web.xml 的配置 总结 公司有个小的内部使用的软件,让开发,自己选择使用 SSM :因为之前自己学过,本以为一切水到渠成,但是好久 ...
- 利用Python进行数据分析_Pandas_数据结构
申明:本系列文章是自己在学习<利用Python进行数据分析>这本书的过程中,为了方便后期自己巩固知识而整理. 首先,需要导入pandas库的Series和DataFrame In [21] ...