【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中给对象添加方法
在JavaScript中,我们经常要给已定义的对象添加一些方法,如下: function circle(w,h){ this.width=w; this.height=h; ...
- LAMP 2.6 Apache 禁止指定user_agent
user_agent 我把它叫做浏览器标识, 目前主流的浏览器有 IE. chrome. Firefox. 360. iphone上的 Safari.Android 手机上的.百度搜索引擎.googl ...
- NDK 编译报错:request for member 'FindClass' in something not a structure or union
ndk编译 xx.c文件时一直报下面的错误: ”request for member 'FindClass' in something not a structure or union ...” 原因 ...
- php学习笔记-定义数组和引用数组元素
上图包含两种定义数组的方法,一种是通过数组索引来创建的,一种是通过array()函数来创建的.
- Ryouko's Memory Note
题目意思:一个书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么|x-y|页都需要翻到(联系生活实际就很容易理解的了).接着有m pieces 的 information ...
- 短信部分之PDU简介及其格式(转)
PDU是大多数手机短信通讯的核心,仅有少数手机只支持Text模式(例如笔者的MOTO C330).PDU模式比起Text模式可以提供能为强大的功能,但其编码较Text模式困难.无论哪种模式,我们都 ...
- Java分层概念(转)
Java分层概念(转) 对于分层的概念,似乎之间简单的三层,多了,就有点难以区分了,所以收藏了这个. ervice是业务层 action层即作为控制器 DAO (Data Access Object) ...
- 20169219《Linux内核原理与分析》第八周作业
网易云课堂学习 task_struct数据结构 struct task_struct { volatile long state;进程状态 void *stack; 堆栈 pid_t pid; 进程标 ...
- javax.servlet.ServletConfig接口(五)
主要作用是保存web.xml文件里面的配置信息 一个servlet对应一个ServletConfig,100个servlet对应100个ServletConfig. 代码如下(单个获取和获取所有) ...
- IIS如何避免子web应用程序中继承根目录web.config配置
1.一种方式,需要改动根目录的web.config(不是很推荐) <?xml version="1.0"?> <configuration> <loc ...