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的更多相关文章

  1. 【leetcode】Combinations (middle)

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

  2. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. select 动态添加option函数

    转自:https://lym6520.iteye.com/blog/309937 经常会用到select动态添加元素,写了个方法,方便调用!  ... /** * 功能:select对象动态添加Opt ...

  2. JAVA中string类的split方法

    split([separator,[limit]])第一个参数为分隔符,可以是一个正则表达式,第二个参数为返回结果数组的长度

  3. zabbix监控报错zabbix server is not running: the information displayed may not be current

    zabbix监控搭建完后打开web界面http://xxx/zabbix报错: zabbix server is not running: the information displayed may ...

  4. 将一个string字符串变量分解为字符输出

    我们定义一个string 变量str ,然后通过str.length()可以获得该字符串变量的长度: #include<iostream> #include<string> u ...

  5. ZROI2018普转提day2t1

    传送门 分析 我们通过仔细研究不难发现对于一次交换(i,i+1)的操作之后,在i之前的点就不可能跑到i之后,i+1之后的的点也不可能跑到i+1之前,所以这个序列在一次交换之后就相当于被分成了两个部分. ...

  6. 巧用 git rebase 将某一部分 commit 复制到另一个分支

    一.为什么需要将一个 commit 复制到其他分支上去呢? 在我们的实际开发的过程中,我们的项目中会存在多个分支. 在某些情况下,可能需要将某一个分支上的 commit 复制到另一个分支上去.   二 ...

  7. 数据结构 queue

    问题描述 t 个团队在餐厅前准备排队. 他们的排队规则是:初始队伍为空.一个人要排进队伍前, 先搜索队伍中是否有他的队友. 如果有, 这名成员就直接站在最后一个队友的后面,如果没有,那么这名成员只能排 ...

  8. springmvc和js前端的数据传递和接收方式

    在springmvc中controller的结果集可通过json格式传到js前端接受,也可以通过Map传给前端,具体实现如下 1,通过json格式传递 controller层实现如下 @Request ...

  9. [译]我们应该在HTML文档中何处放script标签

    本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...

  10. 常用Git命令清单

    我现在工作几乎每天都使用 Git ,但是很多命令记不住. 一般来说,日常使用只要记住下图6个命令,就可以了.但是熟练使用,恐怕要记住60-100个命令. 下面是我整理的常用 Git 命令清单.几个专用 ...