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微信登陆没有回调的原因

    1.在友盟文档中这样说: 链接 7.4 微信登录 添加配置文件参考文档:添加微信及朋友圈,添加相关库文件,配置URL schemes及添加系统回调 注意微信登录必须先在微信开放平台申请微信登录权限 在 ...

  2. Android中EditText,Button等控件的设置

    EditText可以使用:TextView.setEnabled(true)来设置为可编辑,其实很简单,写在这里以便以后自己查看. Button设置可用性:setVisibility(View.VIS ...

  3. 年度酷工作---高级数据工程师(公司靠谱,技术强悍,产品牛叉,福利有干货) 关键词:7000万用户、五星级厨师、住房补助 - V2EX

    年度酷工作---高级数据工程师(公司靠谱,技术强悍,产品牛叉,福利有干货) 关键词:7000万用户.五星级厨师.住房补助 - V2EX 年度酷工作---高级数据工程师(公司靠谱,技术强悍,产品牛叉,福 ...

  4. cocos2d-实现读取.plist文件(使用数组CCArray)

    学习札记之cocos2d-x2.1.1实现读取.plist文件(使用数组CCArray) <?xml version="1.0" encoding="UTF-8&q ...

  5. Linux定义系统提示符的变量:PS1

  6. LVM的一般操作过程

    1. 在磁盘分区上建立物理卷  #fdisk /dev/hdb  #pvdisplay /dev/hdb1 //在已经建立好的分区或硬盘上建立物理卷  #pvcreate /dev/hdb1    2 ...

  7. 关于std::string

    主要注意的一个问题是:std::string 实际是类似一个 vector<char>的结构. 它里面是可以存放 ascii为0 的字符不算结尾 (否则 unicode方式的编码存放就有问 ...

  8. 使用ant的war任务打包j2ee web项目

    <?xml version="1.0" encoding="UTF-8"?> <project name="antwebprojec ...

  9. 自己python程序的并行修改

    遇到运算量大的程序,学习了下python并行运算的方法,在自己的程序上进行了修改,看看是否可以增加效率.原始代码是: import gt_apps as my_apps f=file('sample. ...

  10. 知识点1-2:ASP.NET MVC背景

    1.发展阶段 CGI(公共网关接口)-->ASP(Active Server Pages,活动服务器页面)-->.NET 2. .NET平台 2002年初,微软发布了第一版.NET框架,这 ...