当前位置:网站首页>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);
}
}
}边栏推荐
- Microsoft store can't download apps, vs2019 can't download plug-ins solution
- Monitor the running of server jar and restart script
- 【OBS】P B 丢帧阈值 buffer_duration_usec
- Pythia: Facebook's latest open source visual and language multitasking learning framework
- Form of objects in memory & memory allocation mechanism
- 【StoneDB故障诊断】MDL锁等待
- Inventory Poka ecological potential project | cross chain characteristics to promote the prosperity of multi track
- ApacheSpark-命令执行(CVE-2022-33891) 漏洞复现
- 数组扩容、排序、嵌套语句应用
- Deepfake 换脸真假难辨,马斯克分克已伪装成功
猜你喜欢

Microsoft store can't download apps, vs2019 can't download plug-ins solution
Excalidraw: an easy-to-use online, free "hand drawn" virtual whiteboard tool

Reentranlock and source code analysis (learn ideas and click the source code step by step)

一口气学完 Redis 集群方案

Station B collapsed. If we were the developer responsible for the repair that night

An2021 software installation and basic operation (new file / export)

Pythia: Facebook's latest open source visual and language multitasking learning framework

Apachespark command execution (cve-2022-33891) vulnerability recurrence

Deepfake 换脸真假难辨,马斯克分克已伪装成功

Inventory Poka ecological potential project | cross chain characteristics to promote the prosperity of multi track
随机推荐
Form of objects in memory & memory allocation mechanism
V2.X 同步异常,无法云端同步的帖子一大堆,同步又卡又慢
Import word document pictures blocking and non blocking IO operations
零钱通项目(两个版本)含思路详解
Excalidraw:很好用的在线、免费「手绘」虚拟白板工具
Are Transformers Effective for Time Series Forecasting?|填坑
B站崩了,那晚负责修复的开发人员做了什么?
Tencent cloud [hiflow] | automation --------- hiflow: still copying and pasting?
Software testing interview question: when does the software testing project start? Why?
The US Department of justice added 16 new charges against Huawei, including stealing trade secrets
What is modcount in the source code? What's the effect
MySQL execution process and order
Memo mode - unity
[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)
B站崩了,如果我们是那晚负责修复的开发人员
MySQL data recovery process is based on binlog redolog undo
Seven lines of code crashed station B for three hours
Reentranlock and source code analysis (learn ideas and click the source code step by step)
【OBS】P B 丢帧阈值 buffer_duration_usec
Drawing three coordinate (axis) diagram with MATLAB