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 ...
随机推荐
- 阶段5 3.微服务项目【学成在线】_day17 用户认证 Zuul_14-网关-介绍网关及搭建网关工程
4 Zuul网关 4.1 需求分析 网关的作用相当于一个过虑器.拦截器,它可以拦截多个系统的请求. 本章节要使用网关校验用户的身份是否合法. 4.2 Zuul介绍 什么是Zuul? Spring Cl ...
- C++11消息队列 + Qt线程池 + QRunnable执行任务简单模型
1.模板类queue,包含头文件<queue>中,是一个FIFO队列. queue.push():在队列尾巴增加数据 queue.pop():移除队列头部数据 queue.font():获 ...
- jmeter 和 postman 提交 传输类型为 "multipart/form-data" 的传送写法
Charles抓包: 请求参数Chrome抓包: jmeter写法: postman写法: 脚本的写法见 https://www.cnblogs.com/kaibindirver/p/9870900. ...
- 获取src 内容
获取(代码): ].src; // 测试无效 修改(代码): 1 $("#img").attr('src',path);
- Spring Cloud(6.2):搭建OAuth2 Client
配置web.xml 添加spring-cloud-starter-security,spring-security-oauth2-autoconfigure和spring-boot-starter-o ...
- iOS-self.用法
关于self.用法的一些总结 2010-01-10 21:46 最近有人问我关于什么时候用self.赋值的问题, 我总结了一下, 发出来给大家参考. 有什么问题请大家斧正. 关于什么时间用self. ...
- 分享一个关于Opencv的小总结
import cv2 #opencv读取的格式是BGR import numpy as np 一.#读入文件 img=cv2.imread('cat.jpg') #’’引号内是图片所在盘的地址+名 ...
- (CVE-2015-0240)Samba远程代码执行
简介 Samba 是利用 SMB 协议实现文件共享的一款著名开源工具套件.日前 Samba 曝出一个严重安全漏洞,该漏洞出现在 smbd 文件服务端,漏洞编号为 CVE-2015-0240,可以允许攻 ...
- JVM -- 对象的概述和引用
一.概述 说起垃圾收集(Garbage Collection,GC),大部分人都把这项技术当做java语言的伴生产物,然后GC出现历史比java久远. GC需要完成的3件事情: 1.哪些内存需要回收 ...
- 基于AbstractRoutingDataSource实现动态切换数据源
基于AbstractRoutingDataSource实现动态切换数据源 /** * DataSource注解接口 */ @Target({ElementType.TYPE, ElementTyp ...