算法题丨4Sum
描述
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target?
Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
示例
Given array S = [1, 0, -1, 0, -2, 2], and target = 0.
A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
算法分析
难度:中
分析:给定整型数组和指定一个整型目标值,从整形数组中找出4个不同的元素,使得4个元素之和等于目标值,返回结果为所有满足上述条件的元素组合。
思路:思路可以参考3Sum,主要难度是由原先的找3个元素之和,变成了找4个元素之和。这种情况下,其实有点像解魔方:玩五阶魔方就是从五阶降到四阶,然后再从四阶降到三阶,最后再按照玩三阶魔方的方法解决问题。
最终的解法,其实就是在3阶解法的基础上,在外层再套一层4阶的循环,并做一些基础的判断,复杂度也是在3阶n²基础上*n=n³,当然,这种算法也可推广到n阶。
代码示例(C#)
public IList<IList<int>> FourSum(int[] nums, int target)
{
List<IList<int>> res = new List<IList<int>>();
if (nums.Length < 4) return res;
//排序
Array.Sort(nums);
//4阶判断
for (int i = 0; i < nums.Length - 3; i++)
{
//如果最近4个元素之和都大于目标值,因为数组是排序的,后续只可能更大,所以跳出循环
if (nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) break;
//当前元素和最后3个元素之和小于目标值即本轮最大值都小于目标值,则本轮不满足条件,跳过本轮
if (nums[i] + nums[nums.Length - 1] + nums[nums.Length - 2] + nums[nums.Length - 3] < target)
continue;
//防止重复组合
if (i > 0 && nums[i] == nums[i - 1]) continue;
//3阶判断
for (int j = i + 1; j < nums.Length - 2; j++)
{
//原理同4阶判断
if (nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) break;
if (nums[i] + nums[j] + nums[nums.Length - 1] + nums[nums.Length - 2] < target)
continue;
int lo = j + 1, hi = nums.Length - 1;
while (lo < hi)
{
//已知元素 nums[i],nums[j],剩下2个元素做夹逼
int sum = nums[i] + nums[j] + nums[lo] + nums[hi];
if (sum == target)
{
res.Add(new List<int> { nums[i], nums[j], nums[lo], nums[hi] });
while (lo < hi && nums[lo] == nums[lo + 1]) lo++;
while (lo < hi && nums[hi] == nums[hi - 1]) hi--;
lo++;
hi--;
}
//两边夹逼
else if (sum < target) lo++;
else hi--;
}
}
}
return res;
}
复杂度
- 时间复杂度:O (n³).
- 空间复杂度:O (1).
附录
算法题丨4Sum的更多相关文章
- 算法题丨Two Sum
描述 Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- 算法题丨3Sum
描述 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
- 算法题丨3Sum Closest
描述 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 算法题丨Remove Duplicates from Sorted Array II
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...
- 算法题丨Longest Consecutive Sequence
描述 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...
- 算法题丨Remove Element
描述 Given an array and a value, remove all instances of that value in-place and return the new length ...
- 算法题丨Move Zeroes
描述 Given an array nums, write a function to move all 0's to the end of it while maintaining the rela ...
- 算法题丨Next Permutation
描述 Implement next permutation, which rearranges numbers into the lexicographically next greater perm ...
- 算法题丨Remove Duplicates from Sorted Array
描述 Given a sorted array, remove the duplicates in-place such that each element appear only once and ...
随机推荐
- 文件导出也可以这么写【js+blob】
文件导出在软件开发中是个比较常用的功能,基本原理也很简单: 浏览器向后台发送一个Get请求 后台处理程序接收到请求后,经过处理,返回二进制文件流 浏览器接收到二进制文件流后提示下载文件 调用的js方法 ...
- Django---视图
全过程:用户填写相关数据,提交相关请求,链接到对应的视图上,在此视图上(有用户传过来的数据[就是视图要处理的数据],在视图里面对数据进行业务处理,在数据库中crub数据,然后把对应的界面和界面显示需要 ...
- Unity3D 打包Standalone(exe文件) Shader丢失
Shader丢失算是老生常谈了 从刚开始接触Unity时,从别的地方拿过来模型导入 就认识了一个标志性的颜色 就是粉色,或者是紫色 当在Unity中遇到这种颜色 不用怀疑 绝对是Shader或者材质丢 ...
- 通过Performance Log确定磁盘有性能问题?
一些比较重要的performance counter: Counter Description LogicalDisk\ % Free Space 报告磁盘空间中未被分配的空间占逻辑卷中总可用空间的百 ...
- New Windows 10 SDK - Toast Notification
概述 Toast Notification 在 UWP App 中有很重要的作用,能够很大程度上增强 App 和用户之间的沟通,比如运营推广活动.版本更新.提醒类任务提示等等.Toast Notifi ...
- Objective-C 中的 BOOL
之前开发了一个针对单个数据模型,自动建表.增删改查等操作的 ORM 库,后边在 iPhone 5c 上使用时,出现了 crash 的情况. 该项目在 Github 开源,项目地址为SXJDatabas ...
- spring Boot+spring Cloud实现微服务详细教程第一篇
前些天项目组的大佬跟我聊,说项目组想从之前的架构上剥离出来公用的模块做微服务的开发,恰好去年的5/6月份在上家公司学习了国内开源的dubbo+zookeeper实现的微服务的架构.自己平时对微服务的设 ...
- Object的方法
1.Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象. ES2015引入的 ,且可用polyfilled.要支持旧浏览器的话,可用使用jQ ...
- YCSB测试HBase远程完全分布式集群
写在前面 本文只讲一个很简单的问题,YCSB对HBase集群的测试.虽然网上有很多介绍YCSB测试HBase的文章,但都是针对本地HBase伪分布式集群的.大家都知道,稍微正式一些的压测都会要求测试客 ...
- [poj2585]Window Pains_拓扑排序
Window Pains poj-2585 题目大意:给出一个4*4的方格表,由9种数字组成.其中,每一种数字只会出现在特定的位置,后出现的数字会覆盖之前在当前方格表内出现的.询问当前给出的方格表是否 ...