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],

]

Hide Tags Backtracking

给定一个数n。求1到n之间的全部的k个数的组合。

这个题目能够在纸上画下草图,明显能够用递归求解。递归的终止条件是k=0,而且因为须要将组合保存到集合vector中,还须要使用回溯法来保存数据。

runtime:8ms

class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> result;
vector<int> vec;
helper(1,n,k,vec,result);
return result;
} void helper(int first,int last,int k,vector<int> & vec,vector<vector<int>> & result)
{
if(k==0)
{
result.push_back(vec);
return ;
} for(int i=first;i<=last-k+1;i++)
{
vec.push_back(i);
helper(i+1,last,k-1,vec,result);
vec.pop_back();
}
} };

LeetCode77:Combinations的更多相关文章

  1. LeetCode77. Combinations(剑指offer38-2)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...

  2. Leetcode77. Combinations组合

    给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3] ...

  3. [Swift]LeetCode77. 组合 | Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...

  4. Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  5. [LeetCode] Factor Combinations 因子组合

    Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...

  6. [LeetCode] Combinations 组合项

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  7. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  8. Leetcode 254. Factor Combinations

    Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...

  9. 17. Letter Combinations of a Phone Number

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

随机推荐

  1. iOS NSRuntime机制

    什么是Objective-C runtime? 简单来说,Objective-C runtime是一个实现Objective-C语言的C库.对象可以用C语言中的结构体表示,而方法(methods)可以 ...

  2. ActionBar开启Overlay Mode(覆盖模式)

    以下内容参考自Android官网http://developer.android.com/training/basics/actionbar/overlaying.html#EnableOverlay ...

  3. VS 2013上Python的配置

    最近有点不务正业,去看了下Python (主要是学校OJ有这个语言,然后可以轻松解决大数据问题,不要说我太坑~~~) 目前感觉python和matlab有些类似,缺少了变量类型声明,总感觉自己写出来的 ...

  4. Android面试题整理(1)

    1.Activity的生命周期      onCreate(Bundle saveInstanceState):创建activity时调用.      onStart():activity可见时调用 ...

  5. js 中的cookie

    根据智能社31cookie基础与应用总结, cookie的特性: 1.同一个网站,共用一套cookie,实际上是根据域名来区分的. 如t.sina.com.cn ,和weibo.com这两个都是新浪微 ...

  6. 清华申请退学博士作品:完全用Linux工作,凸Windows

    清华申请退学博士作品:完全用Linux工作 按尽管我们已经不习惯看长篇大论, 但我还是要说, 这是一篇值得你从头读到尾的长篇文章. 2005年9月22日,清华在读博士生王垠在水木社区BLOG上发表了& ...

  7. 模拟红外协议C程序——接收模块

    目的:方便程序的调试,提供效率,减少工作累,可以不在线调试编程时显示实时数据,特别产品不带显示的或者MCU是OPT的,有很大的帮助. 过程:将要看的数据发送出来,另一个板(一个带有显示的就OK了,显示 ...

  8. 一步一步重写 CodeIgniter 框架 (11) —— 使用 CodeIgniter 函数库

    在完成了CI框架的类库扩展后,很自然我们就会想到函数库的扩展.函数库的扩展在 CI 中称为 helper 函数与类有不同的地方,它不能继承,只能覆盖或者添加新的函数,或者直接完全新定义的一组函数. 由 ...

  9. Java NIO与IO

    当学习了Java NIO和IO的API后,一个问题立即涌入脑海: 我应该何时使用IO,何时使用NIO呢?在本文中,我会尽量清晰地解析Java NIO和IO的差异.它们的使用场景,以及它们怎样影响您的代 ...

  10. 不能使用ASP.NET验证控件---WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptRes

    方法一: 在webconfig中找到 <appSettings>        <add key=" aspnet:UseTaskFriendlySynchronizati ...