当前位置:网站首页>Leetcode 18 problem [sum of four numbers] recursive solution
Leetcode 18 problem [sum of four numbers] recursive solution
2022-07-02 05:04:00 【Java full stack R & D Alliance】
Title address : Sum of four numbers
The recursive thinking code is as follows :
But the time-consuming implementation is not to our satisfaction , This is a defect
public static List<List<Integer>> fourSum(int[] nums, int target) {
//set Used to de duplicate the result
Set<String> set = new HashSet<>();
Arrays.sort(nums);
System.out.println(Arrays.toString(nums));
return fourSumToTarget(nums, target, set, 4);
}
private static List<List<Integer>> fourSumToTarget(int[] nums, int target, Set<String> set, int size) {
// Dealing with border situations
if (nums.length < size || size == 0) {
return Collections.emptyList();
}
if ((nums.length == 1 || size == 1) && nums[0] == target) {
return Collections.singletonList(Collections.singletonList(nums[0]));
}
List<List<Integer>> result = new ArrayList<>();
int[] range = Arrays.copyOfRange(nums, 1, nums.length);
// Case one ,nums[0] Belongs to an element in a quadruple
List<List<Integer>> lists = fourSumToTarget(range, target - nums[0], set, size - 1);
for (int i = 0; i < lists.size(); i++) {
List<Integer> integers = lists.get(i);
List<Integer> temp = new ArrayList<>(integers);
temp.add(nums[0]);
if (temp.size() == 4) {
if (set.add(Arrays.toString(temp.toArray()))) {
result.add(temp);
}
} else {
result.add(temp);
}
}
// The second case ,nums[0] An element that does not belong to a quadruple
List<List<Integer>> lists1 = fourSumToTarget(range, target, set, size);
result.addAll(lists1);
return result;
}
}
边栏推荐
- Differential identities (help find mean, variance, and other moments)
- Solution: the agent throws an exception error
- paddle: ValueError:quality setting only supported for ‘jpeg‘ compression
- 2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
- LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
- [bus interface] Axi interface
- CubeMx DMA笔记
- Splice characters in {{}}
- Implementation of go language for deleting duplicate items in sorting array
- Leetcode basic programming: array
猜你喜欢

Knowledge arrangement about steam Education
![[high speed bus] Introduction to jesd204b](/img/78/1a0a3672e63058da6d98da95aa3cf2.jpg)
[high speed bus] Introduction to jesd204b

Starting from the classification of database, I understand the map database

奠定少儿编程成为基础学科的原理

Save the CDA from the disc to the computer

面试会问的 Promise.all()

Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel

正大美欧4的主账户关注什么数据?

AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道

Video cover image setting, put cover images into multiple videos in the simplest way
随机推荐
TypeScript类的使用
Mapping settings in elk (8) es
win10 磁盘管理 压缩卷 无法启动问题
C# 图片显示占用问题
Online incremental migration of DM database
国产全中文-自动化测试软件Apifox
Implementation of go language for deleting duplicate items in sorting array
Design and implementation of general interface open platform - (44) log processing of API services
How to write a client-side technical solution
Promise all()
Pyflink writes MySQL examples with JDBC
農業生態領域智能機器人的應用
农业生态领域智能机器人的应用
Embedded-c language-8-character pointer array / large program implementation
Line by line explanation of yolox source code of anchor free series network (7) -- obj in head_ loss、Cls_ Loss and reg_ Calculation and reverse transmission of loss I
Realize the function of data uploading
Fasttext text text classification
DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
Orthogonal test method and function diagram method for test case design
Johnson–Lindenstrauss Lemma(2)