当前位置:网站首页>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;
}
}
边栏推荐
- [understand one article] FD_ Use of set
- go实现leetcode旋转数组
- idea自動導包和自動删包設置
- Preparation for writing SAP ui5 applications using typescript
- Design and implementation of general interface open platform - (44) log processing of API services
- Rhcsa --- work on the third day
- 案例分享|智慧化的西部机场
- 國產全中文-自動化測試軟件Apifox
- Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
- [high speed bus] Introduction to jesd204b
猜你喜欢
正大留4的主账户信息汇总
Getting started with pytest -- description of fixture parameters
Starting from the classification of database, I understand the map database
解析少儿编程中的动手搭建教程
Mathematical knowledge (Euler function)
el-cascader回显只选中不显示的问题
Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
Save the CDA from the disc to the computer
Common errors of dmrman offline backup
关于Steam 教育的知识整理
随机推荐
LeetCode 1175. 质数排列(质数判断+组合数学)
Go Chan's underlying principles
培养中小学生对教育机器人的热爱之心
正大留4的主账户信息汇总
About PROFIBUS: communication backbone network of production plant
Exercise notes 13 (effective letter ectopic words)
从数组中找出和为目标的下标
Cannot activate CONDA virtual environment in vscode
Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
C # picture display occupancy problem
The underlying principle of go map (storage and capacity expansion)
Pytest learning ----- pytest assertion of interface automation testing
Leetcode basic programming: array
What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
奠定少儿编程成为基础学科的原理
Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
win10 磁盘管理 压缩卷 无法启动问题
oracle 存储过程与job任务设置
A new attribute value must be added to the entity entity class in the code, but there is no corresponding column in the database table
Find the subscript with and as the target from the array