【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 ...
随机推荐
- App启动原理和启动过程
一.程序启动原理 1.1.main函数中执行了一个UIApplicationMain这个函数UIApplicationMain(int argc, char *argv[], NSString ...
- 部署和调优 2.4 tomcat安装
下载tamcet 官网 http://tomcat.apache.org/ 左侧选择版本 复制下载链接 切换到下载目录 cd /usr/local/src linux wget wget http:/ ...
- Result Maps、Auto-mapping、cache
1. Result Maps resultMap元素是Mybatis里面最重要的并且功能最强大的一个元素.(The resultMapelement is the most important an ...
- 无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_PRC_BIN" 之间的排序规则冲
在两个数据库之间进行复合查询时有时会出现如下错误: 无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_PRC_BIN&qu ...
- Codeforces 58E Expression (搜索)
题意:给你一个可能不正确的算式a + b = c, 你可以在a,b,c中随意添加数字.输出一个添加数字最少的新等式x + y = z; 题目链接 思路:来源于这片博客:https://www.cnb ...
- sql编写注意
DROP TABLE IF EXISTS `imooc_pro`; CREATE TABLE `imooc_pro`( `id` int unsigned auto_increment key, `p ...
- linux sdcv命令
一.简介 sdcv全称为stardict console version,是终端下的词典. 二.安装 1)安装sdcv yum install -y sdcv 2)安装字典 http://www. ...
- 283E&EZOJ #89 Cow Tennis Tournament
传送门 分析 我们考虑用所有的情况减去不合法的情况 不难想出所有情况为$C_n^3$ 于是我们考虑不合法的情况 我们知道对于一个不合法的三元组$(a,b,c)$一定是修改后$a<b,b>c ...
- 10.model/view实例(3)
任务:3x2的表格,第一个单元格显示当前时间 思考: 1.data函数里面QTime::currentTime()显示当前时间 2.但是这个事件是一个固定的时间,不会变动 3.需要时间变动,View就 ...
- python3-字典中存储列表
# Auther: Aaron Fan #示例1:#存储所点披萨的信息pizza = { '外皮':'厚的', '配料列表':['香菇', '奶酪'],}#概述所点的比萨print('您点了一道&qu ...