当前位置:网站首页>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();
}
}
}
边栏推荐
- 女生学软件测试难不难 入门门槛低,学起来还是比较简单的
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
- Erreur de type résolue avec succès: type de données « catégorie» non sous - jacente
- My creation anniversary
- 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)
- Data security -- 13 -- data security lifecycle management
- Fedora/rehl installation semanage
- Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
- ROS学习_基础
- UDP攻击是什么意思?UDP攻击防范措施
猜你喜欢

Office doc add in - Online CS

Development of entity developer database application

Leetcode - 152 product maximum subarray

【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例

19. Actual memory management of segment page combination

医疗软件检测机构怎么找,一航软件测评是专家

Leetcode daily question (971. flip binary tree to match preorder traversal)

How to find a medical software testing institution? First flight software evaluation is an expert

(practice C language every day) reverse linked list II

Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
随机推荐
Monotonic stack
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
My creation anniversary
接口自动化测试框架:Pytest+Allure+Excel
[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)
基于PyTorch和Fast RCNN快速实现目标识别
Embed UE4 program into QT interface display
At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
SSO process analysis
A brief introduction of reverseme in misc in the world of attack and defense
[daily question] 729 My schedule I
Office doc add in - Online CS
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
Successfully solved typeerror: data type 'category' not understood
软件测试外包到底要不要去?三年真实外包感受告诉你
Misc of BUU (update from time to time)
开源的网易云音乐API项目都是怎么实现的?
前缀和数组系列
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious