Leetcode77. Combinations组合
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。
示例:
输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
class Solution {
public:
vector<vector<int> >res;
int N;
int K;
vector<vector<int> > combine(int n, int k)
{
if(n == 0 || k > n)
return res;
K = k;
N= n;
vector<int> v;
DFS(1, v, 0);
return res;
}
void DFS(int pos, vector<int> &v, int cnt)
{
if(cnt == K)
{
res.push_back(v);
return;
}
for(int i = pos; i <= N; i++)
{
v.push_back(i);
DFS(i + 1, v, cnt + 1);
v.pop_back();
}
}
};
Leetcode77. Combinations组合的更多相关文章
- [FollowUp] Combinations 组合项
这是Combinations 组合项 的延伸,在这里,我们允许不同的顺序出现,那么新的题目要求如下: Given two integers n and k, return all possible c ...
- [LeetCode] Combinations 组合项
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- LeetCode77:Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 - n. For example, ...
- combinations(组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 【LeetCode每天一题】Combinations(组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- leetCode 77.Combinations (组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [Leetcode] combinations 组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- LeetCode77. Combinations(剑指offer38-2)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- 077 Combinations 组合
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合.例如,如果 n = 4 和 k = 2,组合如下:[ [2,4], [3,4], [2,3], [1,2], [ ...
随机推荐
- String方法之fromCharCode()和charCodeAt()
1.fromCharCode fromCharCode() 可接受一个指定的 Unicode 值,然后返回一个字符串. 语法 我们可以根据 Unicode 来输出 "HELLO" ...
- LINQ学习系列-----3.1 查询非泛型集合和多个分组
一.查询非泛型集合 1.问题起源 LINQ to object在设计时,是配合IEnumerable<T>接口的泛型集合类型使用的,例如字典.数组.List<T>等,但是对于继 ...
- PHP面向对象魔术方法基本了解
简单介绍 (1) 魔术方法都是系统提供,程序员使用即可. (2) 所有的魔术方法,前面都是以 __ 开头的 _是两个下划线. (3) 我们在自定义函数时,就不要使用 __开头了. (4) 魔术方法是 ...
- centos安装gcc4.8.2
1. 下载源码:镜像地址http://mirror.bjtu.edu.cn/gnu/gcc/gcc-4.8.2/gcc-4.8.2.tar.gz用svn下载可以随时更新到最新的版本svn checko ...
- RaspberryPi(一)
[1]格式化TF卡 // 注意格式 [2]烧录系统 // 烧录完成后不要点弹出的击格式化选项 [3]查找IP.修改静态IP(保持和台式机或笔记本同网段) arp -a //物理地址以B8开头 //或者 ...
- java笔试之数字颠倒
描述: 输入一个整数,将这个整数以字符串的形式逆序输出 程序不考虑负数的情况,若数字含有0,则逆序形式也含有0,如输入为100,则输出为001 package test; import java.ut ...
- 实现一个koa-logger中间件
//koa-logger.js module.exports = async(ctx,next)=>{ const start = new Date().getTime() // 中间件异步处理 ...
- 11.Hibernate一对多关系
创建JavaBean 一方: Customer private long cust_id; private String cust_name; private long cust_user_id; p ...
- Python基础——使用with结构打开多个文件
考虑如下的案例: 同时打开三个文件,文件行数一样,要求实现每个文件依次读取一行,然后输出,我们先来看比较容易想到的写法: with open(filename1, 'rb') as fp1: with ...
- ODOO 新API修饰符
Odoo8中,API接口分为traditaional style和record style两种类型: traditional style指的就是我们在7.0中使用的类型,def(self,cr,uid ...