【Leetcode】Combinations
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>> result;
vector<bool> used(n + , false);
vector<int> path;
dfs(n, k, used, path, result);
return result;
}
private:
void dfs(int n, int k, vector<bool> & used, vector<int> &path, vector<vector<int>> &result) {
if (path.size() == k) {
result.push_back(path);
return;
}
for (int i = path.empty() ? : path.back() + ; i <= n - k + + path.size(); ++i) {
if (!used[i]) {
used[i] = true;
path.push_back(i);
dfs(n, k, used, path, result);
path.pop_back();
used[i] = false;
}
}
}
};
与排列类似,只是dfs的时候的搜索的策略稍有不同。
【Leetcode】Combinations的更多相关文章
- 【leetcode】Combinations (middle)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
随机推荐
- Django的Model使用
创建模型 使用Django的模型主要注意两个方面:字段的类型和方法的重写.这里用一个例子来说明,其中包含了常用的字段类型和如何重写方法. from django.db import models cl ...
- sql 一些偶尔会用到的写法和函数 不定时更新
小数转整数: --round() 遵循四舍五入把原值转化为指定小数位数,如: ) -- =1 ) -- =2 --floor() 向下舍入为指定小数位数 如: SELECT floor(1.45) - ...
- go语言linux下安装
1.从http://golang.org/dl/下载最新版本的GO语言二进制档案包. 注意:根据操作系统和计算架构正确选择档案包 2.使用tar命令将档案包解压到/usr/local目录中.具体方法如 ...
- ueditor 1.2.6使用方法
本文以php版本为例: 文件下载:http://ueditor.baidu.com/website/download.html 还可以自己先定义内容,然后下载,这样可以帮助我们精简不少东西. 以本地p ...
- .NET回归 HTML----超文本标记语言(暂时无图)
HTML用来做网页,文件拓展名改为html可以形成网页 超文本标记语言==超越了文字的范畴,除了文字还可以有图片.视频.音频.动画特效等其它内容,由标记符号<>组成的一门计算机编程语言 H ...
- Data Mining: SSE,MSE,RMSE,R-square指标讲解
转载自:http://blog.csdn.net/l18930738887/article/details/50629409 SSE(和方差.误差平方和):The sum of squares due ...
- p3634 [APIO2012]守卫
传送门 分析 1.先预处理出不被0覆盖的点,然后对每个点处理出在它左边离他最近的点和在他右边理他最近的点. 2.对于每个至少存在一个忍者的区间,先将它左右边界处理为不被0所覆盖.排序后将包含其他区间的 ...
- jquery 的插件 extend
让我们来讲解 jquery中的插件机制 $.fn.extend and() $.extend()这两个方法都接受一个参数,类型为Object.Object对象的“名/值对”分别代表“函数或方 ...
- oracle创建数据库的语句
首先 oracle严格来说表空间的概念和数据库的概念很像,为了理解的方便我们,可以把表空间就先当成数据库 我们在安装oracle的服务端的时候默认会安装一些,默认实例 1.建立表空间,现在解释下面语句 ...
- 数据结构_find_lucky_number(寻找幸运值)
数据结构_find_lucky_number(寻找幸运值) 问题描述 给出两个已按升序排列的数组 a[1..n],b[1..m],如果存在 i,j,使得a[i]+b[j]==k,我们便说已找到幸运值. ...