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 ...
随机推荐
- HashSet的实现原理,简单易懂
HashSet的实现原理,简单易懂 答: HashSet实际上是一个HashMap实例,都是一个存放链表的数组.它不保证存储元素的迭代顺序:此类允许使用null元素.HashSet中不允许有重复元 ...
- Qt编写自定义控件44-天气仪表盘
一.前言 天气仪表盘控件是所有控件中唯一一个使用了svg矢量图的控件,各种天气图标采用的矢量图,颜色变换采用动态载入svg的内容更改生成的,其实也可以采用图形字体来做,本次控件为了熟悉下svg在Qt中 ...
- Kafka——副本(Replica)机制
副本定义 Kafka 是有主题概念的,而每个主题又进一步划分成若干个分区.副本的概念实际上是在分区层级下定义的,每个分区配置有若干个副本. 所谓副本(Replica),本质就是一个只能追加写消息的提交 ...
- hdfs中删除文件、文件夹、抓取内容
删除文件 bin/hdfs dfs -rm output2/* 删除文件夹 bin/hdfs dfs -rm -r output2 抓取内容 bin/hdfs dfs -cat /us ...
- python 日志组件
日志组件: import logging import logging.handlers import os class logs(object): def __init__(self): self. ...
- 使用layui的form.on绑定select选中事件, form.on()不执行的原因分析
使用layui的form.on绑定select选中事件中, form.on()不执行, 主要原因有 1, select标签中没有写lay_filter属性,用来监听 <select id=&qu ...
- 【OpenGL学习】 四种绘制直线的算法
我是用MFC框架进行测试的,由于本人也没有专门系统学习MFC框架,代码若有不足之处,请指出. 一,先来一个最简单的DDA算法 DDA算法全称为数值微分法,基于微分方程来绘制直线. ①推导微分方程如下: ...
- python面向对象学习笔记(一)
粘贴一些自学过程中的笔记大纲,源文本在pycharm里面写的,有点乱整理一下,部分内容有待补充,书写不一定100%正确,全当数据备份了. 1.面向对象的特性 #你写代码时什么使用面向对象 #处理比较复 ...
- TiKV事务实现浅析
TiKV事务实现浅析 Percolator事务的理论基础 Percolator的来源 Percolator事务来源于Google在设计更新网页索引的系统时提出的论文Large-scale Increm ...
- [bzoj4665]小w的喜糖_二项式反演
小w的喜糖 题目链接:https://lydsy.com/JudgeOnline/problem.php?id=4665 数据范围:略. 题解: 二项式反演裸题. $f_{i,j}$表示,前$i$种钦 ...