[LeetCode] Combinations [38]
称号
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();
}
}
};
另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
)
版权声明:本文博主原创文章。博客,未经同意不得转载。
[LeetCode] Combinations [38]的更多相关文章
- 【LeetCode算法-38】Count and Say
LeetCode第38题 The count-and-say sequence is the sequence of integers with the first five terms as fol ...
- [LeetCode] 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 ...
- leetcode — combinations
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...
- [leetcode]Combinations @ Python
原题地址:https://oj.leetcode.com/problems/combinations/ 题意:组合求解问题. 解题思路:这种求组合的问题,需要使用dfs来解决. 代码: class S ...
- [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 ...
- leetcode第38题--Combination Sum
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- 【一天一道LeetCode】#38. Count and Say
一天一道LeetCode系列 (一)题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, ...
- LeetCode题解38.Count and Say
38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...
随机推荐
- C:打印菱形(自己的方法)
//-------------------*打印菱形*--------------------- int i,j,k; int n; printf("请输入一个奇数n:"); sc ...
- Cantor的数表 【找规律】
小俞同学,近期勤学苦练数学,对一种数表产生了兴趣. 数表例如以下: 1/1 1/2 1/3 1/4 1/5 2/1 2/2 2/3 2/4 3/1 3/2 3/3 4/1 4/2 5/1 她冥思苦相了 ...
- unity3d由于Camera.main.transform报空引用错误的解决方案
今天在导入character包后,引用了内置的第三人称素材,但是在启动的时候程序报空引用的错误: 引起错误的位置在: 错误原因是因为没有将摄像机的tag设置为maincamera,改为maincame ...
- 每日算法之三十三:Trapping Rain Water
这是一个非常有意思的问题,求解最大容积问题,值得动脑筋想一想. 原题例如以下: Given n non-negative integers representing an elevation map ...
- ecshop首页调用某分类下的商品|assign_cat_goods()
ecshop首页调用分类下的商品其实很简单,也有模板设置那里可以设置,不过那个只可以用cat_goods.lib,不方便,所以我想看看怎么能简单的实现ecshop首页调用分类下的商品 只需要在inde ...
- Struts 2 初学的复习巩固
Q:使用Struts2 开发程序的基本步骤? A: 1)加载Struts2类库: 2)配置web.xml文件,定义核心Filter来拦截用户请求: 3)开发视图层页面,即JSP页面: 4)定义处理用户 ...
- fscanf()功能具体解释
一旦文件被解析常规时间或使用正则表达式.或者是敲自己太傻代码来解析一个普通文件. 今天突然发现c该图书馆有一个现成的文件可以解析常规功能,这是fscanf()功能.哎 曾经自己做了这么多无用功.在这里 ...
- 高仿快车100--实战RadioGroup和RadioButton应用
1.RadioButtonCheckBox的差别: a.单个RadioButton在选中后,通过点击无法变为未选中 单个CheckBox在选中后.通过点击能够变为未选中 b.一组RadioButton ...
- POJ 2738 Two Ends(记忆化)
Description In the two-player game "Two Ends", an even number of cards is laid out in a ro ...
- 重新想象 Windows 8 Store Apps (8) - 控件之 WebView
原文:重新想象 Windows 8 Store Apps (8) - 控件之 WebView [源码下载] 重新想象 Windows 8 Store Apps (8) - 控件之 WebView 作者 ...