当前位置:网站首页>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);
}
}
}
}
边栏推荐
- Explain Huawei's application market in detail, and gradually reduce 32-bit package applications and strategies in 2022
- 【istio简介、架构、组件】
- Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
- Reading notes of pyramid principle
- STM32 serial port register library function configuration method
- Calf problem
- Markdown编辑器Editor.md插件的使用
- 测试人一定要会的技能:selenium的三种等待方式解读,清晰明了
- LeetCode 715. Range module
- ChaosBlade:混沌工程简介(一)
猜你喜欢

Led analog and digital dimming

ESP32-ULP协处理器低功耗模式RTC GPIO中断唤醒

Digital triangle model acwing 1027 Grid access

Output a spiral matrix C language

最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼

STM32 serial port register library function configuration method

H3C VXLAN配置

Two schemes of unit test

【Istio Network CRD VirtualService、Envoyfilter】

External interrupt to realize key experiment
随机推荐
Vagrant failed to mount directory mount: unknown filesystem type 'vboxsf'
Greenplum 6.x build_ install
OpenGL三维图形绘制
数据在内存中的存储
E-commerce campaign Guide
Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - package based on thinkphp5 - detailed guidance of the author - July 6, 2022 - Novice
PMP Exam details after the release of the new exam outline
Simulation volume leetcode [general] 1705 The maximum number of apples to eat
Pointer advanced, string function
年薪50w阿里P8亲自下场,教你如何从测试进阶
PPT模板、素材下载网站(纯干货,建议收藏)
Druid monitoring - Introduction to JMX usage and principle
Led analog and digital dimming
Reflections on the way of enterprise IT architecture transformation (Alibaba's China Taiwan strategic thought and architecture practice)
Count the number of words in the string c language
Interpretation of MySQL optimization principle
Analysis of Hessian serialization principle
【istio简介、架构、组件】
OpenGL frame buffer
Panel display technology: LCD and OLED