当前位置:网站首页>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 successfully resolved: can only use cat accessor with a ‘category‘ dtype
- L'Ia dans les nuages rend la recherche géoscientifique plus facile
- pymongo获取一列数据
- 成功解决TypeError: data type ‘category‘ not understood
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Day 239/300 注册密码长度为8~14个字母数字以及标点符号至少包含2种校验
- Day 248/300 thoughts on how graduates find jobs
- BUU的MISC(不定时更新)
- 【刷题】怎么样才能正确的迎接面试?
- Depth residual network
猜你喜欢

Fedora/REHL 安装 semanage

指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品

Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple

Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet

Suspended else

SQL Server manager studio(SSMS)安装教程
![[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)](/img/3c/c25e7cbef9be1860842e8981f72352.png)
[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)

Introduction and underlying analysis of regular expressions

《从0到1:CTFer成长之路》书籍配套题目(周更)

19. Actual memory management of segment page combination
随机推荐
LeetCode每日一题(1997. First Day Where You Have Been in All the Rooms)
【Hot100】739. 每日温度
[brush questions] how can we correctly meet the interview?
[advanced software testing step 1] basic knowledge of automated testing
19.段页结合的实际内存管理
开源的网易云音乐API项目都是怎么实现的?
My seven years with NLP
The registration password of day 239/300 is 8~14 alphanumeric and punctuation, and at least 2 checks are included
librosa音频处理教程
UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
成功解决TypeError: data type ‘category‘ not understood
BUU的MISC(不定时更新)
Leetcode daily question (971. flip binary tree to match preorder traversal)
Fedora/REHL 安装 semanage
万丈高楼平地起,每个API皆根基
Day 248/300 关于毕业生如何找工作的思考
UniPro甘特图“初体验”:关注细节背后的多场景探索
18.多级页表与快表
MySQL high frequency interview 20 questions, necessary (important)
My creation anniversary