当前位置:网站首页>LeetCode 77:组合
LeetCode 77:组合
2022-07-06 19:10:00 【斯沃福德】
链接
题目:
思路:回溯(无重复元素不可复选)
与子集类似,数据无重复,且(2,1)和(1,2)是一个结果,故不能复选;
子集问题中每一个节点都是子集,但组合问题中当元素个数到达k才将path放入res中!
注意:
集合问题中,给的是数组,for从0到n-1 ;组合问题给的n,for从1到n ;
Java实现:
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); // 注意start从1开始
return res;
}
void dfs(int n,int k,int start){
//终止条件,当元素个数为k则添加
if(path.size()==k){
res.add(new LinkedList(path));
}
for(int i=start;i<=n;i++){
//递归前做选择
path.add(i);
//递归
dfs(n,k,i+1);
//递归后撤销选择
path.removeLast();
}
}
}
边栏推荐
猜你喜欢
软件测试——Jmeter接口测试之常用断言
Linear list --- circular linked list
[Mori city] random talk on GIS data (II)
Apifox,你的API接口文档卷成这样了吗?
The panel floating with the mouse in unity can adapt to the size of text content
MySQL --- 常用函数 - 字符串函数
How to build a 32core raspberry pie cluster from 0 to 1
MySQL提升大量数据查询效率的优化神器
MySQL - common functions - string functions
6-6漏洞利用-SSH安全防御
随机推荐
Unity custom webgl packaging template
[unity notes] screen coordinates to ugui coordinates
Increase 900w+ playback in 1 month! Summarize 2 new trends of top flow qiafan in station B
进程管理基础
wzoi 1~200
Processus général de requête pour PostgreSQL
CDB PDB 用户权限管理
MMDetection3D加载毫米波雷达数据
写作系列之contribution
Douban average 9 x. Five God books in the distributed field!
postgresql之整體查詢大致過程
Detailed explanation of line segment tree (including tested code implementation)
Statistics of radar data in nuscenes data set
What are the characteristics of the operation and maintenance management system
wireshark安装
MySQL
Integerset of PostgreSQL
6-6 vulnerability exploitation SSH security defense
postgresql之整体查询大致过程
STM32 project -- Topic sharing (part)