Combinations [LeetCode]
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],
]
Summary: recursive functions
class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > combinations;
if(n == || k == || n < k)
return combinations;
if(k == ){
for(int i = ; i <= n; i ++) {
vector<int> com(, i);
combinations.push_back(com);
}
return combinations;
}
if(k == n){
vector<int> com;
for(int i = ; i <= n; i ++) {
com.push_back(i);
}
combinations.push_back(com);
return combinations;
}
if(k <= n / ){
for(int i = ; i <= n - k; i ++){
if(i == n - k){
vector<int> com;
for(int j = n - k + ; j <= n; j ++)
com.push_back(j);
combinations.push_back(com);
break;
}
int pick_num = i + ;
vector<vector<int> > sub_com = combine(n - pick_num, k - );
for(auto item : sub_com) {
for(int j = ; j < item.size(); j ++){
item[j] += pick_num;
}
item.insert(item.begin(), pick_num);
combinations.push_back(item);
}
}
return combinations;
}else{
combinations = combine(n, n - k);
vector<vector<int> > counter_combinations;
for(auto item : combinations){
vector<int> com;
int j = ;
for(int i = ; i <= n ; i ++){
if(j < item.size() && item[j] == i)
j ++;
else
com.push_back(i);
}
counter_combinations.push_back(com);
}
return counter_combinations;
}
}
};
Combinations [LeetCode]的更多相关文章
- Combinations ——LeetCode
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- Combinations leetcode java
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- [LeetCode] Factor Combinations 因子组合
Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...
- [LeetCode] Combinations 组合项
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode Factor Combinations
原题链接在这里:https://leetcode.com/problems/factor-combinations/ 题目: Numbers can be regarded as product of ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
随机推荐
- include_path详细解析
include_path详细解析 原文地址:http://www.laruence.com/2010/05/04/1450.html 1.php默认的包含路径为 .;C:\php\pear 即 ...
- TortoiseGit中push的使用
https://tortoisegit.org/docs/tortoisegit/tgit-dug-push.html Options Force (May discard known changes ...
- CSS选择器、优先级与匹配原理(转)
CSS选择器.优先级与匹配原理 导航 为了分析Bootstrap源码,所以的先把CSS选择器相关的东东给巩固好 废话就不多说了 CSS 2.1 selectors, Part 1 计算指定选择器的优先 ...
- JAVA开发-我的第一个webScan扫描器
写的第一句话就是感谢shack2,参考了他的代码知道原来有的解耦可以这样写,但是又在他的基础上改写了很多. 代码分享给大写,下面是程序的截图,我把他取名为:HadesWebScan ps:Wind ...
- CG基础教程-陈惟老师十二讲笔记
转自 麽洋TinyOcean:http://www.douban.com/people/Tinyocean/notes?start=50&type=note 因为看了陈惟十二讲视频没有课件,边 ...
- 构建工具之 - Gradle一般使用常见问答
Gradle借助Groovy语言作为其配置脚本,使得Gradle变的十分强大,几乎是无所不能,我们在掌控这个强大且功能繁多的工具时也比较困难和凌乱,因此本文记录一些使用过程常见的配置,以备不时之需! ...
- Linux基础※※※※如何使用Git in Linux(一)
参考资料: 1. https://www.linux.com/learn/tutorials/796387-beginning-git-and-github-for-linux-users/ 2. h ...
- ASP.Net MVC4中封装CSS和js冗余代码(不让其大篇的显示在前台上)
(1)封装CSS和JS代码,使用调用的方式在前台进行调用.是开发看起来简洁和易于管理,可达到重用. 由于asp.netMVC4 框架 ,在封装js和CSS的时候,有如下规范: using Syst ...
- 进程&线程
进程与线程的区别 什么是进程(Process):普通的解释就是,进程是程序的一次执行,而什么是线程(Thread),线程可以理解为进程中的执行的一段程序片段.在一个多任务环境中下面的概念可以帮助我们理 ...
- Linux下C程序的编译,运行,及调试
先查看linux有没有gcc 和 gdb $ gcc -v $ gdb -v 如果没有安装gcc,可以 $ yum install gcc 要获取管理员权限才能安装软件,$ su root (有的li ...