当前位置:网站首页>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;
}
}
边栏推荐
- Implementation of leetcode two number addition go
- 国产全中文-自动化测试软件Apifox
- VMware installation win10 reports an error: operating system not found
- Differential identities (help find mean, variance, and other moments)
- [bus interface] Axi interface
- Practical problem solving ability of steam Education
- Getting started with pytest -- description of fixture parameters
- Analyzing the hands-on building tutorial in children's programming
- Rhcsa --- work on the fourth day
- Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
猜你喜欢

Vmware安装win10报错:operating system not found
![Introduction to Luogu 3 [circular structure] problem list solution](/img/fd/c0c5687c7e6e74bd5c911b27c3e19c.png)
Introduction to Luogu 3 [circular structure] problem list solution

Save the CDA from the disc to the computer

How to write a client-side technical solution

将光盘中的cda保存到电脑中

Online incremental migration of DM database

数学知识——快速幂的理解及例题

Application d'un robot intelligent dans le domaine de l'agroécologie

Cannot activate CONDA virtual environment in vscode

Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
随机推荐
解决:代理抛出异常错误
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
Mapping location after kotlin confusion
Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy
[understand one article] FD_ Use of set
Comp 250 parsing
[common error] the DDR type of FPGA device is selected incorrectly
Solution: the agent throws an exception error
[quick view opencv] familiar with CV matrix operation with image splicing examples (3)
js中的Map(含leetcode例题)
Idea automatic package import and automatic package deletion settings
画波形图_数字IC
Mathematical knowledge -- understanding and examples of fast power
数学问题(数论)试除法做质数的判断、分解质因数,筛质数
Cubemx DMA notes
删除排序数组中的重复项go语言实现
Basic differences between Oracle and MySQL (entry level)
AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
Analyze the space occupied by the table according to segments, clusters and pages
Ansible installation and use