当前位置:网站首页>Leetcode18题 【四数之和】递归解法
Leetcode18题 【四数之和】递归解法
2022-07-02 05:03:00 【Java全栈研发大联盟】
题目地址: 四数之和
递归思路代码如下:
但是执行耗费时间不合题意,这是缺陷
public static List<List<Integer>> fourSum(int[] nums, int target) {
//set用于对结果去重
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) {
//处理边界情况
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);
//第一种情况,nums[0]属于四元组中的一个元素
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);
}
}
//第二种情况,nums[0]不属于四元组中的一个元素
List<List<Integer>> lists1 = fourSumToTarget(range, target, set, size);
result.addAll(lists1);
return result;
}
}
边栏推荐
- Ruby replaces gem Alibaba image
- List of common bugs in software testing
- Analyzing the hands-on building tutorial in children's programming
- 删除排序数组中的重复项go语言实现
- Video cover image setting, put cover images into multiple videos in the simplest way
- Pyechart1.19 national air quality exhibition
- Preparation for writing SAP ui5 applications using typescript
- leetcode两数相加go实现
- win10 磁盘管理 压缩卷 无法启动问题
- 4. Flask cooperates with a tag to link internal routes
猜你喜欢

10 minute quick start UI automation ----- puppeter

数据库问题汇总

Realize the function of data uploading

Promise all()

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

Summary of database problems
![[common error] the DDR type of FPGA device is selected incorrectly](/img/f3/be66bcfafeed581add6d48654dfe34.jpg)
[common error] the DDR type of FPGA device is selected incorrectly

Rhcsa --- work on the fourth day

Analyzing the hands-on building tutorial in children's programming
![Introduction to Luogu 3 [circular structure] problem list solution](/img/fd/c0c5687c7e6e74bd5c911b27c3e19c.png)
Introduction to Luogu 3 [circular structure] problem list solution
随机推荐
idea自动导包和自动删包设置
Lay the foundation for children's programming to become a basic discipline
正大留4的主账户信息汇总
面试会问的 Promise.all()
LM09丨费雪逆变换反转网格策略
fastText文本分类
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
How to write a client-side technical solution
Rhcsa --- work on the fourth day
Pytest learning ----- pytest assertion of interface automation testing
Differential identities (help find mean, variance, and other moments)
Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
Embedded-c language-8-character pointer array / large program implementation
How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
Implementation of go language for deleting duplicate items in sorting array
Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
Lm09 Fisher inverse transform inversion mesh strategy
解决:代理抛出异常错误
[opencv] image binarization
DJB Hash