当前位置:网站首页>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);
}
}
边栏推荐
- 应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]
- The input input box of H5 page pops up the numeric keypad, which needs to support decimal points
- 软件测试 -- 1 软件测试知识大纲梳理
- 国联证券买股票开户安全吗?
- 直播课堂系统05-后台管理系统
- Resource not found: rgbd_launch 解决方案
- EDA chip design solution based on AMD epyc server
- The concept and operation rules of calculus of variations
- Thymeleaf setting disabled
- Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]
猜你喜欢

45padding won't open the box

Overview of cloud security technology development
![[MySQL must know and know] trigger | permission management](/img/59/cb805d972097a6a8ed7f3ae454a91d.png)
[MySQL must know and know] trigger | permission management

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

Deng Qinglin, a technical expert of Alibaba cloud: Best Practices for disaster recovery and remote multi activity across availability zones on cloud

Realsense ROS installation configuration introduction and problem solving

Idea error failed to determine a suitable driver class

51单片机学习笔记(2)

SSH服务器拒绝了密码

PS making and loading GIF pictures tutorial
随机推荐
[C题目]力扣88. 合并两个有序数组
D2. picking carrots (hard version) (one question per day)
Is it safe for Guolian securities to buy shares and open an account?
Content type corresponding to office file
LeetCode_ String_ Medium_ 151. Reverse the words in the string
EDA chip design solution based on AMD epyc server
Overview of cloud security technology development
Idea error failed to determine a suitable driver class
51 single chip microcomputer learning notes (2)
27 classification of selectors
45padding不会撑开盒子的情况
Leetcode-198- house raiding
Awk from getting started to digging in (23) awk built-in variables argc, argc -- command line parameter transfer
easygui使用的语法总结
直播课堂系统05-后台管理系统
gson与fastjson
Vs2017 large factory ERP management system source code factory general ERP source code
Copy files / folders through Robocopy
物理量与单位符号的书写标准
Quickly set up dobbo demo