当前位置:网站首页>Leetcode 78: subset
Leetcode 78: subset
2022-07-06 07:03:00 【Swarford】
link
subject :
Ideas : to flash back ( No duplicate elements cannot be checked )
Backtracking framework :

This question is form one : No duplicate elements cannot be checked , When writing backtracking algorithms ,for It's from start Start , Not from 0 Start ! namely (2,1) and (1,2) It's a result , Therefore, you cannot check .
use start To ensure that each recursion traverses the number not traversed after the array , Instead of iterating over the previous number
Last strat If the number of arrays exceeds Termination conditions !
Be careful :
Subset problem , Each node is a subset !
Need to be recursive 【 Preamble position 】 take path Add to res ! Instead of adding to the leaf node res !
An empty set [ ] Also operator set 
Java Realization :
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){
// use start To ensure that each recursion traverses the number behind the array , Instead of iterating over the previous number ,
// Last strat Exceeding the number of arrays is the termination condition !
// An empty set [] Also operator set
res.add(new LinkedList(path)); // Each node is a subset ! Instead of adding to the leaf node res !
for(int i=start;i<nums.length;i++){
// Select before recursion
path.add(nums[i]);
// recursive ,i+1 Ensure that the elements in the array are not reused
dfs(nums,i+1);
// Undo after recursion
path.removeLast();
}
}
}
边栏推荐
- 配置树莓派接入网络
- Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
- hydra常用命令
- 医疗软件检测机构怎么找,一航软件测评是专家
- 顶测分享:想转行,这些问题一定要考虑清楚!
- pymongo获取一列数据
- 因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
- leetcode1020. 飞地的数量(中等)
- Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
猜你喜欢

Leetcode - 152 product maximum subarray

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

AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm

Windows Server 2016 standard installing Oracle

3. Business and load balancing of high architecture

ROS2安装及基础知识介绍

OpenGL ES 学习初识(1)

Reflex WMS medium level series 3: display shipped replaceable groups

How to reconstruct the class explosion caused by m*n strategies?

攻防世界 MISC中reverseMe简述
随机推荐
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
ROS2安装及基础知识介绍
What does UDP attack mean? UDP attack prevention measures
Short video, more and more boring?
19.段页结合的实际内存管理
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
BUU的MISC(不定时更新)
Kubernetes cluster builds ZABBIX monitoring platform
OpenGL ES 学习初识(1)
Simple use of JWT
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
SSO process analysis
How to reconstruct the class explosion caused by m*n strategies?
Embed UE4 program into QT interface display
Fast target recognition based on pytorch and fast RCNN
1189. Maximum number of "balloons"
SAP SD发货流程中托盘的管理
编译,连接 -- 笔记 -2
UDP攻击是什么意思?UDP攻击防范措施
《从0到1:CTFer成长之路》书籍配套题目(周更)