当前位置:网站首页>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();
}
}
}
边栏推荐
- 中青看点阅读新闻
- Missing monitoring: ZABBIX monitors the status of Eureka instance
- 这个高颜值的开源第三方网易云音乐播放器你值得拥有
- Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
- 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)
- LeetCode Algorithm 2181. 合并零之间的节点
- SSO process analysis
- The psychological process from autojs to ice fox intelligent assistance
- Database basics exercise part 2
- Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
猜你喜欢
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
首发织梦百度推送插件全自动收录优化seo收录模块
Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file
18.多级页表与快表
因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
How to find a medical software testing institution? First flight software evaluation is an expert
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
Every API has its foundation when a building rises from the ground
机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
随机推荐
The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module
Development of entity developer database application
开源的网易云音乐API项目都是怎么实现的?
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
【Hot100】739. 每日温度
Applied stochastic process 01: basic concepts of stochastic process
BIO模型实现多人聊天
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
leetcode6109. 知道秘密的人数(中等,周赛)
基于PyTorch和Fast RCNN快速实现目标识别
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
How to reconstruct the class explosion caused by m*n strategies?
leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
Wechat official account infinite callback authorization system source code, launched in the whole network
呆错图床系统源码图片CDN加速与破解防盗链功能
Bitcoinwin (BCW): the lending platform Celsius conceals losses of 35000 eth or insolvency
Blue Bridge Cup zero Foundation National Championship - day 20
leetcode59. 螺旋矩阵 II(中等)
TS基础篇
Compile, connect -- notes-2