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. ...
随机推荐
- Highlighting Text Item On Entry In Oracle Forms
Highlight a Text Item in Oracle Forms With Visual Attribute It is very necessary to highlight the cu ...
- [SAP ABAP开发技术总结]EXIT-COMMAND
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 数据库mysql中having 和where的区别
having的用法 having字句可以让我们筛选成组后的各种数据,where字句在聚合前先筛选记录,也就是说作用在group by和having字句前.而 having子句在聚合后对组记录进行筛选. ...
- ubuntu 安装JDK
下载JDK6安装包,我的为32位系统所以选择jdk-6u35-linux-i586.bin 下载地址:http://www.oracle.com/technetwork/java/javase/dow ...
- LTE Module User Documentation(翻译4)—— 使用 Fading Trace
LTE用户文档 (如有不当的地方,欢迎指正!) 7 使用 Fading Trace 本节描述如何在 LTE 仿真中使用 fading traces . (1)生成 Fading Traces ...
- 在.bashrc文件中定义函数
在命令行上直接定义shell函数的明显缺点是当退出shell时,函数就消失了,对于复杂的函数来说,这可能会是个问题. 一个简单的方法就是每次启动新shell的时候都会自动加载所需要的函数. 最好的办法 ...
- [转]-Gradle使用手册(二):项目结构
原文地址:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Using-sourceCompatibility-1. ...
- 自学EF一些小笔记
一直在用DHhelper做MVC,感觉好山寨,也不怎么好用.决定开始学EF. 废话不多说开始记笔记..... EF就是把数据库表,存储过程,视图实例化,通过继承DbContext的一个类来操作数据实例 ...
- JavaWeb学习总结(六)—HttpServletResponse
Response概述: response是Servlet.service方法的一个参数,类型为javax.servlet.http.HttpServletResponse.在客户端发出每个请求时,服务 ...
- 基础1 JavaSe基础
JavaSe基础 1. 九种基本数据类型的大小,以及他们的封装类 boolean 无明确指定 Boolean char 16bits Character byte 8bits Byte short 1 ...