当前位置:网站首页>Leetcode question brushing record (array) combination sum, combination sum II
Leetcode question brushing record (array) combination sum, combination sum II
2022-07-07 09:04:00 【White butterfly】
I haven't scratched the question for a long time , I haven't written a blog for a long time , The strangest thing is that I haven't been busy lately , Autumn moves are just around the corner , This article only records the process of question brushing , Prepare for future review
One 、 Combinatorial summation
This problem is a typical DFS, Search deeply by selecting and not selecting two states , Because a number can be selected unlimited times , So in recursion , There is no need to add one to the subscript
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<Integer> item = new ArrayList<>();
List<List<Integer>> ans = new ArrayList<List<Integer>>();
dfs(candidates,target,item,ans,0);
return ans;
}
public void dfs(int[] candidates, int target,List<Integer> item,List<List<Integer>> ans,int index){
if(index == candidates.length){
return ;
}
if(target == 0){
ans.add(new ArrayList(item));
return ;
}
// No election
dfs(candidates,target,item,ans,index+1);
// choose
if(target-candidates[index] >= 0){
item.add(candidates[index]);
dfs(candidates,target-candidates[index],item,ans,index);
item.remove(item.size()-1);
}
}
}
Two 、 Combinatorial summation II
This question is more restrictive than the previous one , Each number can only be used once , And cannot contain duplicate sets
1、 Use each number once : This only needs to let index++ It can be solved , The current number is selected , You can't choose this number
2、 Cannot contain duplicate sets : We can arrange the array first , In each layer of the loop, only A number , for example :1 1 1 2 2 This group of numbers , If the first one is selected in the first layer of loop 1, Then you can't choose the second 1, The next time you choose to meet the conditions, you can only choose 2, Otherwise, it will cause repeated problems
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<Integer> item = new ArrayList<>();
List<List<Integer>> ans = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
dfs(candidates,target,item,ans,0);
return ans;
}
public void dfs(int[] candidates, int target,List<Integer> item,List<List<Integer>> ans,int index){
if(target == 0){
ans.add(new ArrayList(item));
return ;
}
for(int i = index ; i <candidates.length ; i++){
if(i!=index&&candidates[i]==candidates[i-1]){
// Prevent the same layer from circulating and selecting the same number
continue;
}
if(target - candidates[i] >= 0){
item.add(candidates[i]);
dfs(candidates,target-candidates[i],item,ans,i+1);
item.remove(item.size()-1);
}
}
}
}
边栏推荐
- Simulation volume leetcode [general] 1557 The minimum number of points that can reach all points
- Markdown编辑器Editor.md插件的使用
- Port occupation troubleshooting
- OpenGL帧缓冲
- Selenium automation integration, eight years of testing experience, soft test engineer, an article to teach you
- 年薪50w阿里P8亲自下场,教你如何从测试进阶
- Original collection of hardware bear (updated on June 2022)
- Simulation volume leetcode [general] 1609 Parity tree
- Pointer advanced, string function
- LeetCode 715. Range module
猜你喜欢

On December 8th, 2020, the memory of marketing MRC application suddenly increased, resulting in system oom

Platformization, a fulcrum of strong chain complementing chain

Serial port experiment - simple data sending and receiving

PMP Exam Preparation experience systematically improve project management knowledge through learning

Pointer advanced, string function

H3C VXLAN配置

2021 year end summary

Upgrade Alibaba cloud RDS (relational database service) instance to com mysql. jdbc. exceptions. Troubleshooting of jdbc4.communicationsexception

MySQL master-slave delay solution

C语言指针(习题篇)
随机推荐
[chaosblade: node disk filling, killing the specified process on the node, suspending the specified process on the node]
2020 year end summary
What are the suggestions for PMP candidates?
channel. Detailed explanation of queuedeclare parameters
外部中断实现按键实验
Cmake command line use
Golang etcdv3 reports an error. The attribute in grpc does not exist
【Istio Network CRD VirtualService、Envoyfilter】
xray的简单使用
Serial port experiment - simple data sending and receiving
【ChaosBlade:节点 CPU 负载、节点网络延迟、节点网络丢包、节点域名访问异常】
LeetCode 736. LISP syntax parsing
Original collection of hardware bear (updated on June 2022)
Hard core sharing: a common toolkit for hardware engineers
Screen automatically generates database documents
Original collection of hardware bear (updated on May 2022)
JVM 内存结构 详细学习笔记(一)
Lenovo hybrid cloud Lenovo xcloud: 4 major product lines +it service portal
Greenplum 6.x monitoring software setup
Reading notes of pyramid principle