当前位置:网站首页>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);
}
}
}
}
边栏推荐
- 硬件大熊原创合集(2022/05更新)
- Cmake command line use
- Markdown编辑器Editor.md插件的使用
- Original collection of hardware bear (updated on May 2022)
- Synchronized underlying principle, volatile keyword analysis
- Systick滴答定时器
- go mod module declares its path as: gtihub. com/xxx-xx but was required as:xx-xx
- How to count the number of project code lines
- Pointer advanced, string function
- Expérience de port série - simple réception et réception de données
猜你喜欢
LeetCode 736. Lisp 语法解析
H3C vxlan configuration
UnityShader入门精要个人总结--基础篇(一)
ESP32-ULP协处理器低功耗模式RTC GPIO中断唤醒
寄存器地址名映射
Markdown编辑器Editor.md插件的使用
Platformization, a fulcrum of strong chain complementing chain
2022-07-06 unity core 9 - 3D animation
The longest ascending subsequence model acwing 1017 Strange thief Kidd's glider
Goldbach conjecture C language
随机推荐
Leetcode刷题记录(数组)组合总和、组合总和 II
Troublesome problem of image resizing when using typora to edit markdown to upload CSDN
Simulation volume leetcode [general] 1706 Where does the ball meet
PMP Exam details after the release of the new exam outline
模拟卷Leetcode【普通】1705. 吃苹果的最大数目
Output a spiral matrix C language
PPT模板、素材下载网站(纯干货,建议收藏)
C语言指针(上篇)
What are the conditions for applying for NPDP?
Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up
寄存器地址名映射
Calf problem
C语言指针(特别篇)
MAC OSX php dyld: Library not loaded: /usr/local/xxxx. dylib
LeetCode 715. Range module
LeetCode 736. LISP syntax parsing
模拟卷Leetcode【普通】1609. 奇偶树
MySQL主从延迟的解决方案
LeetCode 736. Lisp 语法解析
Simulation volume leetcode [general] 1557 The minimum number of points that can reach all points