LeetCode_Combinations
2024-10-10 01:24:15
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,
If n = 4 and k = 2, a solution is: [
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
class Solution {
private:
vector<vector<int>> result;
vector<int> a ;
public:
void findResult(int n, int k, int num,int start )
{
if(num == k)
{
result.push_back(a);
return;
}
for(int i = start; i <= n; i++)
{ a[num] = i;
findResult( n, k, num+, i+); }
}
vector<vector<int> > combine(int n, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function a.resize(k);
result.clear();
findResult( n, k, , ) ;
return result; }
};
重写后:
//Combinations
class Solution {
public:
void DFS(int n, int start,int k, vector<int> &ans){
if(ans.size() == k){
res.push_back(ans);
return;
}
for(int i = start; i <= n; i++)
{
ans.push_back(i);
DFS(n, i+, k, ans);
ans.pop_back();
}
}
vector<vector<int> > combine(int n, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
res.clear();
vector<int> ans;
DFS(n, , k, ans);
return res; }
private:
vector<vector<int>> res;
};
最新文章
- JavaScript进阶之路(一)初学者的开始
- ubuntu中常用软件的安装
- Dockerfile完成Hadoop2.6的伪分布式搭建
- Flex Excel下载
- 关于JAVA多线程的那些事__初心者
- [转]epoll技术
- selenium2.0处理case实例(一)
- C51编译器扩展的关键词 &; C51中断函数的写法
- MC-设置 止盈
- 用Left join代替not in
- lua lua解读
- C语言可变參函数的实现
- 【原创】公司各个阶段 CTO 需要做什么?(上篇)
- Concept of function continuity in topology
- NOIAC41 最短路(线性基)
- Hexo:创建属于你自己的博客
- 使用 Docker 部署 Grafana + Prometheus 监控 MySQL 数据库
- LOJ-10105(欧拉回路模板,套圈法,递归)
- MySQL 安装 用户管理 常用命令
- list转化为json数组