当前位置:网站首页>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;
}
}
边栏推荐
- 7.1模拟赛总结
- 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
- Win10 disk management compressed volume cannot be started
- Introduction to Luogu 3 [circular structure] problem list solution
- Application of intelligent robot in agricultural ecology
- 初学爬虫-笔趣阁爬虫
- LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
- DMA Porter
- 洛谷入门3【循环结构】题单题解
- 奠定少儿编程成为基础学科的原理
猜你喜欢
Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
Online incremental migration of DM database
[understand one article] FD_ Use of set
Summary of common string processing functions in C language
Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
el-cascader回显只选中不显示的问题
List of common bugs in software testing
Detailed process of DC-1 range construction and penetration practice (DC range Series)
[opencv] image binarization
随机推荐
Mapping settings in elk (8) es
Splice characters in {{}}
【ClickHouse】How to create index for Map Type Column or one key of it?
Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
Preparation for writing SAP ui5 applications using typescript
Record my pytorch installation process and errors
Summary of MySQL key challenges (2)
6.30 year end summary, end of student age
Mapping location after kotlin confusion
Comp 250 parsing
How to write a client-side technical solution
oracle 存储过程与job任务设置
TypeScript类的使用
Rhcsa --- work on the third day
设置滚动条默认样式 谷歌浏览器
Gin framework learning code
Find the subscript with and as the target from the array
C# 基于MQTTNet的服务端与客户端通信案例
农业生态领域智能机器人的应用
[bus interface] Axi interface