No.018: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.
For example, 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]
]
官方难度:
Medium
翻译:
给定一个长度为n的无序数组S和目标数字target,在数组S中找出4个整数a,b,c,d,使a+b+c+d=target。
找出所有的可能解,且解集中不包含重复项。
例子:
数组S:{ 1, 0, -1, 0, -2, 2},目标值target=0。
解集为[ [-1,0,0,1],[-2,-1,1,2],[-2,0,0,2] ]。
- 显然,本题是No.001(Two Sum),No.015(3Sum),No.016(3Sum Closest)更深一步的讨论。在解决这道问题时,不妨考虑一种解法,适用于5Sum,6Sum之后一系列的问题。我可以将这些问题总结归纳为:kSum问题,k>=2。
- 显然是需要使用递归,而递归的终点就是2Sum问题。
- 将递归方法独立出来,在进入递归之前,优先给数组排序(如k=2,这种做法会略微影响性能)。
- 2Sum问题,仍然使用夹逼的原则,同时维护两侧的previous值。增加优化策略:当前左值大于目标数且当前左值大于0,return;当2倍前左值大于目标值,或2倍当前右值小于目标值,return。
- 当k>2时,做类似2Sum的优化策略,将当前目标值-当前值,作为递归入参的目标值,同时传入当前索引值,进行递归。
- 获得递归的解集,循环加入当前值到解集中返回。
- 注意入参检查。
解题代码:
public static List<List<Integer>> fourSum(int[] nums, int target) {
if (nums == null || nums.length < 4) {
throw new IllegalArgumentException("Input error");
}
Arrays.sort(nums);
return kSum(nums, 0, target, 4);
}
private static List<List<Integer>> kSum(int[] nums, int startIndex, int target, int kSum) {
List<List<Integer>> result = new LinkedList<>();
// 递归终点是2Sum问题
if (kSum == 2) {
int left = startIndex, right = nums.length - 1;
int preLeft = Integer.MIN_VALUE, preRight = Integer.MIN_VALUE;
while (left < right) {
if (nums[left] > target && nums[left] > 0) {
return result;
}
if (2 * nums[left] > target || 2 * nums[right] < target) {
return result;
}
if (nums[left] == preLeft) {
left++;
continue;
}
if (nums[right] == preRight) {
right--;
continue;
}
int sum = nums[left] + nums[right];
if (sum == target) {
List<Integer> list = new LinkedList<>();
list.add(nums[left]);
list.add(nums[right]);
result.add(list);
}
if (sum < target) {
preLeft = nums[left];
left++;
} else {
preRight = nums[right];
right--;
}
}
} else {
// 大于2Sum问题,使用递归
int previous = Integer.MAX_VALUE;
for (int i = startIndex; i < nums.length - 1; i++) {
if (nums[i] > target && nums[i] > 0) {
return result;
}
// target值的范围超过k个极值
if (kSum * nums[i] > target || kSum * nums[nums.length - 1] < target) {
return result;
}
if (nums[i] == previous) {
continue;
}
int tempTarget = target - nums[i];
// 开启递归
List<List<Integer>> tempResult = kSum(nums, i + 1, tempTarget, kSum - 1);
for (List<Integer> a : tempResult) {
a.add(nums[i]);
result.add(a);
}
previous = nums[i];
}
}
return result;
}
fourSum
相关链接:
https://leetcode.com/problems/4sum/
PS:如有不正确或提高效率的方法,欢迎留言,谢谢!
No.018:4Sum的更多相关文章
- Python练习题 018:打印星号菱形
[Python练习题 018] 打印出如下图案(菱形): * *** ***** ******* ***** *** * --------------------------------------- ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- C++笔记018:构造函数的调用规则
原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 一.默认构造函数 两个特殊的构造函数 1.默认无参构造函数 当类中没有定义构造函数时,编译器默认提供一个无参构造函数,并且其函数体为空 ...
- Python3练习题 018:打印星号菱形
Python的内置方法 str.center(width [, fillchar]) 就能轻而易举打印出来:str即是数量不等的星号,width即是最大宽度(7个空格),默认填充字符fillchar就 ...
- 018:InnoDB 存储引擎、表空间
目录 一.InnoDB 存储引擎 1. InnoDB的历史 2. InnoDB的特点 3. InnoDB存储引擎的文件 3.1 概述 3.2 InnoDB - 表空间 3.3 General表空间 3 ...
- LeetCode OJ:4Sum(4数字之和)
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- 018:include函数详解
include函数详解(了解——虽然用的很少): include函数的用法,目前有三种使用方式: 1.include(module,namespace=None): module:子url的模块字符串 ...
- LeetCode18: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 ...
- ELK学习实验018:filebeat收集docker日志
Filebeat收集Docker日志 1 安装docker [root@node4 ~]# yum install -y yum-utils device-mapper-persistent-data ...
随机推荐
- Python魔法 - MetaClass
Python魔法 - MetaClass metaclass The class of a class. Class definitions create a class name, a class ...
- leancloud 手机注册用户(调用API) 教程
// 从storybord 连线过来的button方法(注册按钮) - (IBAction)regist:(UIButton *)sender { AFHTTPSessionManager *mana ...
- 快速入门系列--WCF--08扩展与新特性
最后一章将进行WCF扩展和新特性的学习,这部分内容有一定深度,有一个基本的了解即可,当需要自定义一个完整的SOA框架时,可以再进行细致的学习和实践. 服务端架构体系的构建主要包含接下来的几个要素:服务 ...
- Html与CSS快速入门03-CSS基础应用
这部分是html细节知识的学习. 快速入门系列--HTML-01简介 快速入门系列--HTML-02基础元素 快速入门系列--HTML-03高级元素和布局 快速入门系列--HTML-04进阶概念 边框 ...
- Objective-C中@property的所有属性详解
1,assign : 简单赋值,不更改索引计数 假设你用malloc分配了一块内存,并且把它的地址赋值给了指针a,后来你希望指针b也共享这块内存,于是你又把a赋值给(assign)了b.此时a 和b指 ...
- CentOS yum安装Apache + PHP + Tomcat7 + MySQL
Linux平台上用得最多的web环境就是php.java和MySQL了,会搭建这个环境,就能把很多开源程序跑起来. 作为一个程序猿,虽然并不用精通运维的活,但基本的Linux环境搭建还是要掌握比较好, ...
- Android抓包方法(一)之Fiddler代理
Android抓包方法(一) 之Fiddler代理 前言: 做前端测试,基本要求会抓包,会分析请求数据包,查看接口是否调用正确,数据返回是否正确,问题产生是定位根本原因等. 不管是之前做HTML5手机 ...
- Windows Azure Web Site (1) 用户手册
<Windows Azure Platform 系列文章目录> 下载地址: Web Apps用户手册
- 安装logstash,elasticsearch,kibana三件套
logstash,elasticsearch,kibana三件套 elk是指logstash,elasticsearch,kibana三件套,这三件套可以组成日志分析和监控工具 注意: 关于安装文档, ...
- Moon转告给你一个比Log4net更好日志框架--TracerX Logger 及其对应的日志查看器
一.介绍 TracerX logger是一个易于上手,且拥有众多高级特性的.NET日志框架. 它能够发送输出结果到多目的地(循环文件.事件日志等....).它也能生成文本和二进制文件.它拥有一个强大的 ...