当前位置:网站首页>Leetcode combination sum + pruning
Leetcode combination sum + pruning
2022-07-25 14:53:00 【Nuy.oah】
Leetcode39. Combinatorial summation
To give you one No repeating elements Array of integers for candidates And a target integer target , find candidates You can make the sum of numbers as the target number target Of all Different combinations , And return... As a list . You can press In any order Return these combinations .
Input :candidates = [2,3,6,7], target = 7
Output :[[2,2,3],[7]]
explain :
2 and 3 Can form a set of candidates ,2 + 2 + 3 = 7 . Be careful 2 You can use it multiple times .
7 Is also a candidate , 7 = 7 .
Only these two combinations .

Pictured , First, there are four options , Can be in 2,3,6.7 Choose one of them , Then add it to the temporary list , After that, there are four selection methods for each layer ( Because it can be selected repeatedly ), After each selection, we use sum+= Selected value , When we choose until sum be equal to target When , It shows that we have found a situation that meets the condition , Then we will add this temporary list to the result list , If sum>target, It means that the current value should not be selected , direct return that will do . Only when sumtarget when , Can proceed , Traversing an array of integers , Then add the current value , to flash back , revoke .
public static void main(String[] args) {
int[] candidates={
2,3,6,7};
int target=7;
List<List<Integer>> list = combinationSum(candidates, target);
System.out.println(list);
}
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> list=new ArrayList<>();
List<Integer> path=new ArrayList<>();
Arrays.sort(candidates);// You have to sort , Prevent duplication
dfs(list,candidates,0,0,target,path);
return list;
}
private static void dfs(List<List<Integer>> list, int[] nums, int start, int sum, int target, List<Integer> path) {
if(sum==target){
list.add(new ArrayList<>(path));
return;
}
if (sum>target)// not essential
return;
for (int i = start; (i < nums.length&&sum+nums[i]<=target); i++) {
// If sum Greater than target There is no need to cycle
path.add(nums[i]);
dfs(list,nums,i,sum+nums[i],target,path);
path.remove(path.size()-1);
}
}
边栏推荐
- 37 element mode (inline element, block element, inline block element)
- D2. picking carrots (hard version) (one question per day)
- SSM framework integration, simple case
- How many ways can you assign initial values to a two-dimensional array?
- 51 single chip microcomputer learning notes (1)
- Examples of bio, NiO, AIO
- H5页面input输入框弹起数字键盘,需要支持小数点
- 快速搭建Dobbo小Demo
- [MySQL must know and know] trigger | permission management
- [C topic] the penultimate node in the Niuke linked list
猜你喜欢
![[MySQL series] - how much do you know about the index](/img/d7/5045a846580be106e2bf16d7b30581.png)
[MySQL series] - how much do you know about the index

Ssh server rejected password
![[MySQL must know and know] trigger | permission management](/img/59/cb805d972097a6a8ed7f3ae454a91d.png)
[MySQL must know and know] trigger | permission management

06. Neural network like

Syntax summary of easygui

Heyuan City launched fire safety themed milk tea to boost fire prevention and control in summer

I2C设备驱动程序的层次结构

PS making and loading GIF pictures tutorial

【MySQL必知必会】触发器 | 权限管理

EDA chip design solution based on AMD epyc server
随机推荐
How many ways can you assign initial values to a two-dimensional array?
[C题目]力扣88. 合并两个有序数组
Splice a field of the list set into a single string
PS making and loading GIF pictures tutorial
各种平台dpkg包下载地址(包括arm64)
44 Sina navigation, Xiaomi sidebar exercise
QObject源码剖析-d指针和q指针
I2C设备驱动程序的层次结构
spark参数调整调优
Paddlenlp之UIE关系抽取模型【高管关系抽取为例】
41 picture background synthesis - colorful navigation map
37 元素模式(行内元素,块元素,行内块元素)
Gameframework making games (II) making UI interface
MySQL的登陆【数据库系统】
Go语言创始人从Google离职
35 快速格式化代码
I hope some suggestions on SQL optimization can help you who are tortured by SQL like me
35 quick format code
006操作符简介
PHP implements non blocking (concurrent) request mode through native curl