77. Combinations (Recursion)
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],
]
思路:遍历n以内的正整数,每个数有放入与不放入两种选择=>带回溯的递归
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> result;
vector<int> pre;
dfs(result,pre,n,k,);
return result;
}
void dfs( vector<vector<int>> &result, vector<int> pre, int n, int k, int depth)
{
if(depth > n) return;
pre.push_back(depth);
if(pre.size() < k)
{
dfs(result,pre,n,k,depth+);
}
else
{
result.push_back(pre);
}
pre.pop_back();
dfs(result,pre,n,k,depth+);
}
};
77. Combinations (Recursion)的更多相关文章
- [LeetCode] 77. Combinations 全组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- Leetcode 77, Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 77. Combinations
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...
- 77. Combinations(medium, backtrack, 重要, 弄了1小时)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 【一天一道LeetCode】#77. Combinations
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- [leetcode]77. Combinations组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- 77. Combinations (java 求C(n,k)的组合,排除重复元素)
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 解析:同求全 ...
- (效率低下)77. Combinations C++回溯法 组合
https://leetcode.com/problems/combinations/ 沿用78题的思路 class Solution { public: void backTrack(vector& ...
- LeetCode OJ 77. Combinations
题目 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exa ...
随机推荐
- L196 Hospital educations
Surprisingly,no one knows how many children receive education in English hospitals,still less the co ...
- start-stop-daemon自动启动、关闭后台程序参数传递
/************************************************************************* * start-stop-daemon自动启动.关 ...
- 常见OJ评判结果对照表
Waiting:你的程序刚刚提交,正在等待OJ评测你的程序. Compiling:OJ正在编译你的程序. Accepted:OK!你的程序是正确的 ^_^. Wrong Answer:你的 ...
- DesignPattern(二) 创建型模式
创建型模式 创建型模式就是用来创建对象的模式,抽象了实例化的过程.所有的创建型模式都有两个共同点.第一,它们都将系统使用哪些具体类的信息封装起来:第二,它们隐藏了这些类的实例是如何被创建和组织的.创建 ...
- Cockpit 容器&&kubernetes 管理可视化工具
安装 在k8s 的master 上 yum install -y cockpit cockpit-ws cockpit-kubernetes cockpit-bridge cockpit-dashbo ...
- C语言Socket编程(计算机网络作业)
最近我计算机网络课程要做作业了,没办法跟着老师一步一步的写C语言的代码,使用的计算就是Socket通信发送消息:代码实现的功能很简单,客户端向服务器端发送消息,服务器端接收客户端发来的消息,并且输出显 ...
- php 双引号字符串里包变量的用法
第一种 $ary['a'] 去掉单引号$ary = array('a'=>1);$b = 5; $str = "fdsfdsf$ary[a]";$str = "fd ...
- Linux进程间通信——使用共享内存(转)
一.什么是共享内存 顾名思义,共享内存就是允许两个不相关的进程访问同一个逻辑内存.共享内存是在两个正在运行的进程之间共享和传递数据的一种非常有效的方式.不同进程之间共享的内存通常安排为同一段物理内存. ...
- WeX5 苹果APP打包教程
来源:http://docs.wex5.com/app-packing/ WeX5平台App打包教程 WeX5是前端快速开发框架,可开发跨端运行应用,是移动App/微信/WebApp开发利器,一次开发 ...
- Winfrom 开源组件Control.FirefoxDialog使用
1. 如果窗体是以模式窗体方式打开的,会出现点了应用,窗体就立马关闭.此时可能别的设置需要一块设置,这种就存在问题. var form1 = new Form1(); form1.ShowDialog ...