当前位置:网站首页>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();
}
}
}
边栏推荐
- Simple use of MySQL database: add, delete, modify and query
- Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
- Setting and using richview trvstyle template style
- BIO模型实现多人聊天
- ROS2安装及基础知识介绍
- AI on the cloud makes earth science research easier
- 这个高颜值的开源第三方网易云音乐播放器你值得拥有
- 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
- UDP攻击是什么意思?UDP攻击防范措施
- C语言_双创建、前插,尾插,遍历,删除
猜你喜欢
Huawei equipment configuration ospf-bgp linkage
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
Missing monitoring: ZABBIX monitors the status of Eureka instance
leetcode59. 螺旋矩阵 II(中等)
Reflex WMS medium level series 3: display shipped replaceable groups
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
18. Multi level page table and fast table
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
Chapter 7 - thread pool of shared model
随机推荐
Cif10 actual combat (resnet18)
Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
树莓派串口登录与SSH登录方法
Proteus -- Serial Communication parity flag mode
leetcode59. 螺旋矩阵 II(中等)
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
Blue Bridge Cup zero Foundation National Championship - day 20
医疗软件检测机构怎么找,一航软件测评是专家
BUU的MISC(不定时更新)
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
Fedora/rehl installation semanage
Chapter 7 - thread pool of shared model
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
Apache dolphin scheduler source code analysis (super detailed)
18.多级页表与快表
开源的网易云音乐API项目都是怎么实现的?
接口自动化测试框架:Pytest+Allure+Excel
kubernetes集群搭建Zabbix监控平台
漏了监控:Zabbix对Eureka instance状态监控