当前位置:网站首页>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();
}
}
}
边栏推荐
- Pallet management in SAP SD delivery process
- The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module
- [some special grammars about C]
- PCL实现选框裁剪点云
- NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
- Bio model realizes multi person chat
- Depth residual network
- Three methods of adding color to latex text
- Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
- 指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
猜你喜欢

Database basics exercise part 2

作者已死?AI正用艺术征服人类

BUU的MISC(不定时更新)

巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...

Development of entity developer database application

Thought map of data warehouse construction

ROS学习_基础

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

Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file
![[brush questions] how can we correctly meet the interview?](/img/89/a5b874ba4db97fbb3d330af59c387a.png)
[brush questions] how can we correctly meet the interview?
随机推荐
UDP攻击是什么意思?UDP攻击防范措施
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
Development of entity developer database application
Windows Server 2016 standard installing Oracle
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
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
19.段页结合的实际内存管理
3. Business and load balancing of high architecture
Blue Bridge Cup zero Foundation National Championship - day 20
GET 和 POST 请求类型的区别
LeetCode Algorithm 2181. 合并零之间的节点
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
19. Actual memory management of segment page combination
树莓派3B更新vim
leetcode59. 螺旋矩阵 II(中等)
The difference between get and post request types
What is the difference between int (1) and int (10)? Senior developers can't tell!
C语言_双创建、前插,尾插,遍历,删除
CDN acceleration and cracking anti-theft chain function
前缀和数组系列