当前位置:网站首页>Leetcode刷题记录(数组)组合总和、组合总和 II
Leetcode刷题记录(数组)组合总和、组合总和 II
2022-07-07 06:28:00 【白蝶丶】
真是好久没刷题,好久没写博客了,最怪的是最近也没忙什么,秋招在即,此篇仅记录刷题过程,为以后复习做准备
一、组合总和
这道题是个典型的DFS,通过选和不选两种状态进行深搜,由于一个数可以不限次数的选取,所以在递归中,下标无需加一
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 ;
}
//不选
dfs(candidates,target,item,ans,index+1);
//选
if(target-candidates[index] >= 0){
item.add(candidates[index]);
dfs(candidates,target-candidates[index],item,ans,index);
item.remove(item.size()-1);
}
}
}
二、组合总和 II
这道题比上题多加了个限制,就是每一个数只能用一次,并且不能包含重复的集合
1、每一个数用一次:这个只需要在每次递归时让index++就能解决,选取了当前数,就不能在选这个数了
2、不能包含重复的集合:这个我们可以先将数组排好序,在每一层循环中只能选择一种数,例如:1 1 1 2 2 这组数,在第一层循环中如果选择了第一个1,那么就不能选择第二个1,下次选择满足条件的话就只能选择2,不然就会造成重复的问题
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]){
//防止同一层循环宣选到一样的数
continue;
}
if(target - candidates[i] >= 0){
item.add(candidates[i]);
dfs(candidates,target-candidates[i],item,ans,i+1);
item.remove(item.size()-1);
}
}
}
}
边栏推荐
- Alibaba P8 teaches you how to realize multithreading in automated testing? Hurry up and stop
- Uniapp wechat applet monitoring network
- 2022-07-06 unity core 9 - 3D animation
- 模拟卷Leetcode【普通】1567. 乘积为正数的最长子数组长度
- ncs成都新電面試經驗
- Chaosblade: introduction to chaos Engineering (I)
- Several methods of calculating the average value of two numbers
- Greenplum 6.x reinitialization
- Markdown editor Use of MD plug-in
- Simple use of Xray
猜你喜欢

Troublesome problem of image resizing when using typora to edit markdown to upload CSDN

ncs成都新電面試經驗

NCS Chengdu Xindian interview experience

MySQL主从延迟的解决方案
![Data analysis methodology and previous experience summary 2 [notes dry goods]](/img/e1/643e847a777e1effcbd39f8b009e2b.png)
Data analysis methodology and previous experience summary 2 [notes dry goods]

Markdown编辑器Editor.md插件的使用

数字三角形模型 AcWing 1027. 方格取数

C语言指针(中篇)

Routing information protocol rip

面试题:高速PCB一般布局、布线原则
随机推荐
Explain Huawei's application market in detail, and gradually reduce 32-bit package applications and strategies in 2022
systemd
[chaosblade: delete pod according to the tag, pod domain name access exception scenario, pod file system i/o failure scenario]
Several common database connection methods
STM32的时钟系统
Simulation volume leetcode [general] 1609 Parity tree
数字三角形模型 AcWing 275. 传纸条
阿里p8推荐,测试覆盖率工具—Jacoco,实用性极佳
H3C vxlan configuration
Simulation volume leetcode [general] 1706 Where does the ball meet
模拟卷Leetcode【普通】1705. 吃苹果的最大数目
C语言指针(上篇)
Screen automatically generates database documents
JS operation
Markdown编辑器Editor.md插件的使用
2020 year end summary
Ppt template and material download website (pure dry goods, recommended Collection)
Vagrant failed to mount directory mount: unknown filesystem type 'vboxsf'
Synchronized underlying principle, volatile keyword analysis
RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (5 x 5). Kernel size c