当前位置:网站首页>Leetcode-39-total number of combinations
Leetcode-39-total number of combinations
2022-07-27 22:11:00 【benym】
# LeetCode-39- Total number of combinations
Given an array without repeating elements candidates And a target number target , find candidates All can make numbers and target The combination of .
candidates The number in can be selected repeatedly without limit .
explain :
- All figures ( Include
target) Positive integers . - A solution set cannot contain duplicate combinations .
Example 1:
Input : candidates = [2,3,6,7], target = 7,
The solution set is :
[
[7],
[2,2,3]
]Example 2:
Input : candidates = [2,3,5], target = 8,
The solution set is :
[
[2,2,2,2],
[2,3,3],
[3,5]
]# Their thinking
Method 1、DFS+ to flash back :
The problem of permutation , Generally, it is also a full permutation problem
This kind of problem can be traced + Recursive solution
The basic idea :
- When target=7 when , Root node , Make branch selection
- When it comes to 0 Or negative numbers , It's the leaf node , Go back
- When it comes to 0 when , Save the result set , From the root node to the leaf node (0) The path of is one of the combinations of answers
At first, you must draw a recursive tree on the paper , Write the entire tree structure of the example , After understanding how to implement the most basic algorithm , According to the meaning of the question, we need to consider the problem that the results are not repeated , So we need to prune the tree
For example 1 in , Possible paths are [[2,2,3],[2,3,2],[3,2,2],[7]]
Only [[7],[2,2,3]], obviously , The reason for repetition is that the previously selected elements are considered in the deeper node values
Pruning process :
- When searching , You need to set the subscript of the starting point
start, Avoid selecting previously selected nodes
# Java Code
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
int len = candidates.length;
if(len==0) return res;
List<Integer> arr = new ArrayList<>();
Arrays.sort(candidates);
backtrack(candidates,target,res,0,arr);
return res;
}
public void backtrack(int[] candidates,int target,List<List<Integer>> res,int i,List<Integer> tmp_list){
if(target<0) return;
if(target==0){
res.add(new ArrayList<>(tmp_list));
return;
}
// If start from 0 Start , Will traverse all arrays , Select duplicate , Record start Avoid repetition
for(int start=i;start<candidates.length;start++){
if(target<0) break;
tmp_list.add(candidates[start]);
backtrack(candidates,target-candidates[start],res,start,tmp_list);
tmp_list.remove(tmp_list.size()-1);
}
}
}边栏推荐
- 8000字讲透OBSA原理与应用实践
- Read Plato farm's eplato and the reason for its high premium
- Interview question: what are the functions of fail safe mechanism and fail fast mechanism
- ApacheSpark-命令执行(CVE-2022-33891) 漏洞复现
- V2.x synchronization is abnormal. There are a lot of posts that cannot be synchronized in the cloud, and the synchronization is blocked and slow
- [C language] high precision addition, subtraction, multiplication and division template
- Talk about MySQL transaction two-phase commit
- The design idea of relational database is obvious to you in 20 pictures
- What is modcount in the source code? What's the effect
- How to deal with high concurrency deadlock?
猜你喜欢

@Can component be used on the same class as @bean?

If demand splitting is as simple as cutting a cake | agile practice

每条你收藏的资讯背后,都离不开TA

7行代码让B站崩溃3小时

Monitor the running of server jar and restart script

Huaneng Fujian company and Huawei digital energy deepen strategic cooperation and jointly build a low-carbon smart county

Log4j 漏洞仍普遍存在,并持续造成影响

学完4种 Redis 集群方案要多久?我一口气给你说完

Deepfake's face is hard to distinguish between true and false, and musk Fenke has disguised successfully

It seems to be a bug of thread pool, but I think the source code design is unreasonable.
随机推荐
基于简化的评分卡、Smote采样和随机森林的信贷违约预测
【StoneDB故障诊断】系统资源瓶颈诊断
2021-11-05理解main方法语法、代码块及final关键字
Matplotlib 多子图绘制
Matlab 绘制风速、风向统计玫瑰花图
An2021 software installation and basic operation (new file / export)
Software testing interview question: what is the focus of unit testing, integration testing, and system testing?
STM32 project Sharing -- mqtt intelligent access control system (including app control)
Why do server programs need to listen first
Are Transformers Effective for Time Series Forecasting?|填坑
vs2019 release模式调试:此表达式有副作用,将不予计算。
Array expansion, sorting, nested statement application
【海洋科学】海洋气候指数【Climate Indices】数据集
Encapsulate an array into a class
Common shortcut keys and setting methods of idea
What is modcount in the source code? What's the effect
day 1 - day 4
How long will it take to learn the four redis cluster solutions? I'll finish it for you in one breath
Go language learning notes - mutex start go language from scratch
JVM garbage collection garbage collector and common combination parameters