当前位置:网站首页>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);
}
}
}
}
边栏推荐
- LeetCode 736. LISP syntax parsing
- Digital triangle model acwing 1027 Grid access
- UnityShader入门精要个人总结--基础篇(一)
- How to count the number of project code lines
- Golang etcdv3 reports an error. The attribute in grpc does not exist
- 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
- Markdown editor Use of MD plug-in
- 数据在内存中的存储
- C语言指针(中篇)
- Count the number of words C language
猜你喜欢
![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]

数字三角形模型 AcWing 275. 传纸条

Serial port experiment - simple data sending and receiving

C语言指针(中篇)

NCS Chengdu Xindian interview experience

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

Markdown editor Use of MD plug-in

UnityShader入门精要个人总结--基础篇(一)

使用Typora编辑markdown上传CSDN时图片大小调整麻烦问题

On December 8th, 2020, the memory of marketing MRC application suddenly increased, resulting in system oom
随机推荐
Reflections on the way of enterprise IT architecture transformation (Alibaba's China Taiwan strategic thought and architecture practice)
寄存器地址名映射
LeetCode 736. Lisp 语法解析
[wechat applet: cache operation]
9c09730c0eea36d495c3ff6efe3708d8
channel. Detailed explanation of queuedeclare parameters
【Istio Network CRD VirtualService、Envoyfilter】
Greenplum 6.x reinitialization
LeetCode 715. Range module
Personal deduction topic classification record
【istio简介、架构、组件】
Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
STM32串口寄存器库函数配置方法
【Istio Network CRD VirtualService、Envoyfilter】
Data analysis methodology and previous experience summary 2 [notes dry goods]
Common operating commands of Linux
The longest ascending subsequence model acwing 1017 Strange thief Kidd's glider
串口实验——简单数据收发
数字三角形模型 AcWing 1027. 方格取数
[istio introduction, architecture, components]