当前位置:网站首页>Leetcode 77: combination
Leetcode 77: combination
2022-07-07 02:50:00 【Swarford】
link
subject :
Ideas : to flash back ( No duplicate elements cannot be checked )
Similar to subsets , No duplication of data , And (2,1) and (1,2) It's a result , Therefore, you cannot check ;
Every node in the subset problem is a subset , But when the number of elements reaches k Only then path Put in res in !
Be careful :
In the set problem , It's an array ,for from 0 To n-1 ; The combination problem gives n,for from 1 To n ;
Java Realization :
class Solution {
List<List<Integer>> res=new LinkedList<>();
LinkedList<Integer> path=new LinkedList<>();
public List<List<Integer>> combine(int n, int k) {
dfs(n,k,1); // Be careful start from 1 Start
return res;
}
void dfs(int n,int k,int start){
// Termination conditions , When the number of elements is k Then add
if(path.size()==k){
res.add(new LinkedList(path));
}
for(int i=start;i<=n;i++){
// Make a choice before recursion
path.add(i);
// recursive
dfs(n,k,i+1);
// Undo selection after recursion
path.removeLast();
}
}
}
边栏推荐
- What management points should be paid attention to when implementing MES management system
- AWS learning notes (I)
- The panel floating with the mouse in unity can adapt to the size of text content
- 如何设计好接口测试用例?教你几个小技巧,轻松稿定
- 从控制理论的角度谈数据分析
- The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
- 哈希表及完整注释
- [Mori city] random talk on GIS data (II)
- Station B's June ranking list - feigua data up main growth ranking list (BiliBili platform) is released!
- Redis入门完整教程:复制拓扑
猜你喜欢
PSINS中19维组合导航模块sinsgps详解(时间同步部分)
Have fun | latest progress of "spacecraft program" activities
Station B's June ranking list - feigua data up main growth ranking list (BiliBili platform) is released!
Redis入门完整教程:客户端管理
1 -- Xintang nuc980 nuc980 porting uboot, starting from external mx25l
Matlb| economic scheduling with energy storage, opportunity constraints and robust optimization
C language exercises_ one
一文读懂Faster RCNN
Redis入門完整教程:問題定比特與優化
Niuke programming problem -- double pointer of 101 must be brushed
随机推荐
一文读懂Faster RCNN
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (filtering part)
Redis入门完整教程:AOF持久化
电气工程及其自动化
What are the applications and benefits of MES management system
Error in fasterxml tostringserializerbase
Dotconnect for DB2 Data Provider
Wireshark installation
软件测试——Jmeter接口测试之常用断言
Five reasons for clothing enterprises to deploy MES management system
Compress JS code with terser
Cloud Mail . NET Edition
How to write test cases for test coupons?
The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
Safety delivery engineer
如何设计好接口测试用例?教你几个小技巧,轻松稿定
Google Earth Engine(GEE)——Landsat 全球土地调查 1975年数据集
LeetCode 77:组合
Use of fiddler
Contribution of Writing Series