当前位置:网站首页>LeetCode 78:子集
LeetCode 78:子集
2022-07-06 06:55:00 【斯沃福德】
链接
题目:
思路:回溯(无重复元素不可复选)
回溯框架:

此题是形式一: 无重复元素不可复选,写回溯算法的时候,for就要从start开始,而不是从0开始! 即(2,1)和(1,2)是一个结果,故不能复选。
用start来保证每次递归时遍历数组后面未遍历到的数,而不会重复遍历前面的数
最后strat超过数组个数即为终止条件!
注意:
子集问题中,每个节点都是子集 !
需要在递归的【前序位置】将path添加到res !而不是到叶子节点才添加到res !
空集 [ ] 也算子集
Java实现:
class Solution {
List<List<Integer>> res=new LinkedList<>();
LinkedList<Integer> path=new LinkedList<>();
public List<List<Integer>> subsets(int[] nums) {
if(nums==null){
return new List<List<Integer>>;
}
dfs(nums,0);
return res;
}
void dfs(int[] nums,int start){
//用start来保证每次递归时遍历数组后面的数,而不会重复遍历前面的数,
//最后strat超过数组个数即为终止条件!
// 空集[]也算子集
res.add(new LinkedList(path)); // 每个节点都是子集 !而不是到叶子节点才添加到res !
for(int i=start;i<nums.length;i++){
//递归前选择
path.add(nums[i]);
//递归,i+1确保数组中元素不被重复使用
dfs(nums,i+1);
//递归后撤销
path.removeLast();
}
}
}
边栏推荐
- Erreur de type résolue avec succès: type de données « catégorie» non sous - jacente
- 26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
- Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
- Proteus -- Serial Communication parity flag mode
- 中青看点阅读新闻
- [hot100] 739. Température quotidienne
- Entity Developer数据库应用程序的开发
- SAP SD发货流程中托盘的管理
- hydra常用命令
- Development of entity developer database application
猜你喜欢

顶测分享:想转行,这些问题一定要考虑清楚!
![[unity] how to export FBX in untiy](/img/03/b7937a1ac1a677f52616186fb85ab3.jpg)
[unity] how to export FBX in untiy

接口自动化测试实践指导(上):接口自动化需要做哪些准备工作

开源的网易云音乐API项目都是怎么实现的?

因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫

Machine learning plant leaf recognition

AI on the cloud makes earth science research easier

Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL

ROS2安装及基础知识介绍

On the first day of clock in, click to open a surprise, and the switch statement is explained in detail
随机推荐
C语言_双创建、前插,尾插,遍历,删除
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
Apache dolphin scheduler source code analysis (super detailed)
BUU的MISC(不定时更新)
UniPro甘特图“初体验”:关注细节背后的多场景探索
Proteus -- Serial Communication parity flag mode
ROS学习_基础
BIO模型实现多人聊天
C language_ Double create, pre insert, post insert, traverse, delete
Classification des verbes reconstruits grammaticalement - - English Rabbit Learning notes (2)
Refer to how customer push e-commerce does content operation
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
软件测试外包到底要不要去?三年真实外包感受告诉你
Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
Fast target recognition based on pytorch and fast RCNN
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
《从0到1:CTFer成长之路》书籍配套题目(周更)
Lesson 7 tensorflow realizes convolutional neural network