称号

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 {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > ret;
helper(k, 1, n, vector<int>(), ret);
return ret;
}
void helper(int k, int start, int n, vector<int> part, vector<vector<int> > &ret){
if(k==part.size()){
ret.push_back(part);
return;
}
for(int i=start; i<=n; ++i){
part.push_back(i);
helper(k, i+1, n, part, ret);
part.pop_back();
}
}
};
假设你认为本篇对你有收获,请帮顶。

另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
你能够搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/30761165
)

版权声明:本文博主原创文章。博客,未经同意不得转载。

[LeetCode] Combinations [38]的更多相关文章

  1. 【LeetCode算法-38】Count and Say

    LeetCode第38题 The count-and-say sequence is the sequence of integers with the first five terms as fol ...

  2. [LeetCode] Combinations 组合项

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

  3. LeetCode——Combinations

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

  4. leetcode — combinations

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...

  5. [leetcode]Combinations @ Python

    原题地址:https://oj.leetcode.com/problems/combinations/ 题意:组合求解问题. 解题思路:这种求组合的问题,需要使用dfs来解决. 代码: class S ...

  6. [LeetCode] Combinations (bfs bad、dfs 递归 accept)

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

  7. leetcode第38题--Combination Sum

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C  ...

  8. 【一天一道LeetCode】#38. Count and Say

    一天一道LeetCode系列 (一)题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, ...

  9. LeetCode题解38.Count and Say

    38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...

随机推荐

  1. Hdu 4539 【状态DP】.cpp

    题意: 一个炮兵可以攻打和他之间曼哈顿距离为2的士兵,给出你一块n*m的战场,告诉你哪些地方可以站人哪些地方不可以,问你最多可以安放多少个士兵? n <= 100, m <= 10 思路: ...

  2. 关于margin-right的应用问题

    今天在群里面,有人抛出了一个关于css中margin-right没有效果的问题.CSS代码和HTML代码例如以下: .style1{ width:400px; height:440px; backgr ...

  3. Python重写C语言程序100例--Part6

    ''' [程序41] 题目:学习static定义静态变量的使用方法 1.程序分析: 2.程序源码: ''' # python没有这个功能了,仅仅能这样了:) def varfunc(): var = ...

  4. 64位CentOS5.6安装Mysql 5.5.11GA

    1.更新并查看当前CentOS版本是否为5.6yum updatelsb_release -a 2.下载文件下载 bison-2.4.3.tar.gz到/usr/local/src下载 cmake-2 ...

  5. app服务器

    http://heipark.iteye.com/blog/1847421http://heipark.iteye.com/blog/1847421http://wenku.baidu.com/vie ...

  6. 扩展WebBrowser控件,使其支持拖放文件

    public partial class UserControl1 : WebBrowser { private const int WmDropfiles = 0x233; [DllImport(& ...

  7. 使用Visual Studio 2010 创建简单的Silverlight应用程序

    使用Visual Studio 2010 创建简单的Silverlight应用程序 Silverlight是创建动态的引人的RIAs(Rich Internet Application)的新方法.这里 ...

  8. 勾股数组及其应用uva106

    勾股数组 设三元组(a,b,c)满足a^2 + b^2 = c^2的勾股数组,那么是否存在无穷多个勾股数组呢, 答案是肯定的,将三元组乘以d,可以得到新的三元组(da,db,dc) 即(da)^2 + ...

  9. poj1182食物链(种类并查集)

    http://poj.org/problem?id=1182 r[x] = 0 表示x和父亲是同类r[x] = 1 表示x吃父亲r[x] = 2 表示x被父亲吃因为只存在三种动物,且三种动物构成了环形 ...

  10. RMQ之ST算法

    #include <stdio.h> #include <string.h> ; int a[N]; ]; inline int min(const int &a, c ...