当前位置:网站首页>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;
}
}
边栏推荐
- The underlying principle of go map (storage and capacity expansion)
- How to modify data file path in DM database
- js中的Map(含leetcode例题)
- Comp 250 parsing
- Exercise notes 13 (effective letter ectopic words)
- Online incremental migration of DM database
- 【ClickHouse】How to create index for Map Type Column or one key of it?
- 农业生态领域智能机器人的应用
- 画波形图_数字IC
- 设置滚动条默认样式 谷歌浏览器
猜你喜欢
面试会问的 Promise.all()
Solution: the agent throws an exception error
CubeMx DMA笔记
Differential identities (help find mean, variance, and other moments)
Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy
Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
Mathematical knowledge -- understanding and examples of fast power
[high speed bus] Introduction to jesd204b
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
Lm09 Fisher inverse transform inversion mesh strategy
随机推荐
Knowledge arrangement about steam Education
4. Flask cooperates with a tag to link internal routes
Use of Baidu map
Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
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
面试会问的 Promise.all()
7.1模拟赛总结
Mathematical knowledge -- understanding and examples of fast power
C case of communication between server and client based on mqttnet
fastText文本分类
Learn BeanShell before you dare to say you know JMeter
How to recover deleted data in disk
Video cover image setting, put cover images into multiple videos in the simplest way
The El cascader echo only selects the questions that are not displayed
How to write a client-side technical solution
[Yu Yue education] autumn 2021 reference materials of Tongji University
【ClickHouse】How to create index for Map Type Column or one key of it?
Video multiple effects production, fade in effect and border background are added at the same time
Detailed process of DC-1 range construction and penetration practice (DC range Series)
List of common bugs in software testing