当前位置:网站首页>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);
}
}
}
}
边栏推荐
- Count the number of words in the string c language
- 【Istio Network CRD VirtualService、Envoyfilter】
- Why is access to the external network prohibited for internal services of the company?
- Greenplum 6.x monitoring software setup
- 外部中断实现按键实验
- OpenGL帧缓冲
- Greenplum 6.x build_ Environment configuration
- 寄存器地址名映射
- 年薪50w阿裏P8親自下場,教你如何從測試進階
- xray的简单使用
猜你喜欢

寄存器地址名映射

面试题:高速PCB一般布局、布线原则

Greenplum 6.x build_ install

Markdown editor Use of MD plug-in

C语言指针(上篇)

Full link voltage test of the e-commerce campaign Guide

Platformization, a fulcrum of strong chain complementing chain

Oracle makes it clear at one time that a field with multiple separators will be split into multiple rows, and then multiple rows and columns. Multiple separators will be split into multiple rows, and

Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up

Greenplum 6.x version change record common manual
随机推荐
LED模拟与数字调光
9c09730c0eea36d495c3ff6efe3708d8
Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
数据在内存中的存储
Expérience de port série - simple réception et réception de données
Calculation s=1+12+123+1234+12345 C language
C语言指针(中篇)
Upgrade Alibaba cloud RDS (relational database service) instance to com mysql. jdbc. exceptions. Troubleshooting of jdbc4.communicationsexception
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
面板显示技术:LCD与OLED
Two schemes of unit test
Interview question: general layout and wiring principles of high-speed PCB
C语言指针(下篇)
2022-07-06 Unity核心9——3D动画
[chaosblade: node CPU load, node network delay, node network packet loss, node domain name access exception]
Cmake command line use
With an annual salary of 50W, Alibaba P8 will come out in person to teach you how to advance from testing
Panel display technology: LCD and OLED
GoLand set goproxy
Hard core sharing: a common toolkit for hardware engineers