当前位置:网站首页>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;
}
}
边栏推荐
- 设置滚动条默认样式 谷歌浏览器
- Map in JS (including leetcode examples)
- js中的Map(含leetcode例题)
- 奠定少儿编程成为基础学科的原理
- 初学爬虫-笔趣阁爬虫
- JS interview collection test question 1
- [bus interface] Axi interface
- Oracle和MySQL的基本区别(入门级)
- Use of typescript classes
- The reason why sizeof (ARR) / sizeof (arr[0]) is used in the function to calculate the length of the array is incorrect
猜你喜欢

Differential identities (help find mean, variance, and other moments)

DMA Porter

Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?

Unity particle Foundation
![[bus interface] Axi interface](/img/ee/95ade7811ec2c37fb67a77f0b6ae2a.jpg)
[bus interface] Axi interface

Rhcsa --- work on the fourth day
![[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

Simple and practical accounting software, so that accounts can be checked

Realize the function of data uploading

Hcip day 17
随机推荐
AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
Starting from the classification of database, I understand the map database
CubeMx DMA笔记
从数组中找出和为目标的下标
國產全中文-自動化測試軟件Apifox
Detailed process of DC-1 range construction and penetration practice (DC range Series)
Differential identities (help find mean, variance, and other moments)
06 decorator mode
C# 基于MQTTNet的服务端与客户端通信案例
[quick view opencv] familiar with CV matrix operation with image splicing examples (3)
Lay the foundation for children's programming to become a basic discipline
Find the subscript with and as the target from the array
Implementation of leetcode two number addition go
Case sharing | intelligent Western Airport
Orthogonal test method and function diagram method for test case design
DMA Porter
Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
MMAP zero copy knowledge point notes
数学知识——快速幂的理解及例题
Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy