当前位置:网站首页>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();
}
}
}
边栏推荐
- Introduction to ins/gps integrated navigation type
- MetaForce原力元宇宙佛萨奇2.0智能合约系统开发(源码部署)
- The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
- QT common Concepts-1
- Hash table and full comments
- MySQL
- 换个姿势做运维!GOPS 2022 · 深圳站精彩内容抢先看!
- Digital scrolling increases effect
- 你不可不知道的Selenium 8种元素定位方法,简单且实用
- 1 -- Xintang nuc980 nuc980 porting uboot, starting from external mx25l
猜你喜欢
How to write test cases for test coupons?
Cloud Mail .NET Edition
Django database (SQLite) basic introductory tutorial
MATLB|具有储能的经济调度及机会约束和鲁棒优化
基于ensp防火墙双击热备二层网络规划与设计
Station B's June ranking list - feigua data up main growth ranking list (BiliBili platform) is released!
Hash table and full comments
NuScenes数据集关于Radar数据的统计
哈希表及完整注释
MMDetection3D加载毫米波雷达数据
随机推荐
左程云 递归+动态规划
安德鲁斯—-多媒体编程
Code line breaking problem of untiy text box
Redis入门完整教程:客户端案例分析
Hash table and full comments
Unity使用MaskableGraphic画一条带箭头的线
从零安装Redis
QPushButton-》函数精解
Apifox,你的API接口文档卷成这样了吗?
The so-called consumer Internet only matches and connects industry information, and does not change the industry itself
Safety delivery engineer
Unity custom webgl packaging template
数字滚动增加效果
MATLB|具有储能的经济调度及机会约束和鲁棒优化
一文读懂Faster RCNN
惯导标定国内外研究现状小结(删减版)
MySQL - common functions - string functions
从控制理论的角度谈数据分析
Have fun | latest progress of "spacecraft program" activities
QT常见概念-1