当前位置:网站首页>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();
}
}
}
边栏推荐
- 26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
- Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
- 18. Multi level page table and fast table
- [brush questions] how can we correctly meet the interview?
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
- [English] Grammar remodeling: the core framework of English Learning -- English rabbit learning notes (1)
- How to reconstruct the class explosion caused by m*n strategies?
- 漏了监控:Zabbix对Eureka instance状态监控
- C语言_双创建、前插,尾插,遍历,删除
- Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
猜你喜欢
ROS2安装及基础知识介绍
LeetCode每日一题(971. Flip Binary Tree To Match Preorder Traversal)
ROS学习_基础
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
Proteus -- Serial Communication parity flag mode
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
Classification des verbes reconstruits grammaticalement - - English Rabbit Learning notes (2)
医疗软件检测机构怎么找,一航软件测评是专家
Do you really know the use of idea?
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
随机推荐
从autojs到冰狐智能辅助的心里历程
Number of query fields
Visitor tweets about how you can layout the metauniverse
女生学软件测试难不难 入门门槛低,学起来还是比较简单的
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
【刷题】怎么样才能正确的迎接面试?
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
SQL Server Manager studio (SSMS) installation tutorial
PCL实现选框裁剪点云
Day 248/300 关于毕业生如何找工作的思考
[Yu Yue education] Dunhuang Literature and art reference materials of Zhejiang Normal University
Attributeerror successfully resolved: can only use cat accessor with a ‘category‘ dtype
Monotonic stack
攻防世界 MISC中reverseMe简述
Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
18. Multi level page table and fast table
LeetCode每日一题(1997. First Day Where You Have Been in All the Rooms)
Refer to how customer push e-commerce does content operation
将ue4程序嵌入qt界面显示
Delete external table source data