【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 ...
随机推荐
- Javascript面向对象(二):构造函数的继承
这个系列的第一部分,主要介绍了如何"封装"数据和方法,以及如何从原型对象生成实例. 今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个" ...
- JVM知识点总览
jvm 总体梳理 jvm体系总体分四大块: 类的加载机制 jvm内存结构 GC算法 垃圾回收 GC分析 命令调优 当然这些知识点在之前的文章中都有详细的介绍,这里只做主干的梳理 这里画了一个思维导图, ...
- JAVA基础知识总结12(多线程)
进程:正在进行中的程序.其实进程就是一个应用程序运行时的内存分配空间. 线程:其实就是进程中一个程序执行控制单元,一条执行路径.进程负责的是应用程序的空间的标示.线程负责的是应用程序的执行顺序. 一个 ...
- C连接oracle数据库
int db_conn_main() { EXEC SQL BEGEIN DECLARE SECTION; +]; +]; +]; varchar username[]; varchar passwo ...
- sqlplus 连接数据库执行SP
.bashrc export HOME= export LANG="C" .bash_profile #明码 #export LOG_USER=lhcx #export LOG_P ...
- SpringBoot05 数据操作02 -> JPA接口详解
概览 JpaRepository 继承 PagingAndSortingRepository 继承 CrudRepository 继承 Repository 1 Repository 这是一个空接口, ...
- DOM 操作属性
DOM操作就是针对对象的操作 先写一个按钮,<input tupe="button" value="" id="id"> 这 ...
- WOJ 41 约数统计
只会写60分算法QuQ 考虑到一个数$x$大于$\sqrt{x}$的质因数最多只有一个,我们可以筛出小于$\sqrt{r}$范围内的所有质因数然后直接用这些取分解质因数. 最后扫一遍发现还没有分解完的 ...
- 创建Mat对象
Mat 是一个非常优秀的图像类,它同时也是一个通用的矩阵类,可以用来创建和操作多维矩阵.有多种方法创建一个 Mat 对象. 1.构造函数方法 下面是一个使用构造函数创建对象的例子. 常用的构造函数 2 ...
- 自定义select样式
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...