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. ...
随机推荐
- java 导出Excel 大数据量,自己经验总结!
出处: http://lyjilu.iteye.com/ 分析导出实现代码,XLSX支持: /** * 生成<span style="white-space: normal; back ...
- 02_Spring控制反转案例快速入门
Spring控制反转案例快速入门 1.下载Spring最新开发包 http://www.springsource.org/download/community 下载spring3.2 的开发包 目录结 ...
- jquery动态样式操作
获取与设置样式 获取class和设置class都可以使用attr()方法来完成.例如使用attr()方法来获取p元素的class,JQuery代码如下: 1 var p_class = $(" ...
- iOS - OC NSValue 值
前言 @interface NSValue : NSObject <NSCopying, NSSecureCoding> 将任意数据类型包装成 OC 对象 1.比较两个 NSValue 类 ...
- QQLogin
import java.awt.*; import javax.swing.*; public class QQLogin extends JFrame{ QQLogin(){ this.setSiz ...
- js中cookie操作
js中操作Cookie的几种常用方法 * cookie中存在域的概念,使用path和domain区分: * 在同一域中的set和del可以操作同一名称的cookie,但不在同一域中的情况下,则set无 ...
- web设计经验<七>13步打造优雅的WEB字体
今天,大多数浏览器已经默认支持Web字体,日趋增多的字体特性被嵌入最新版HTML和CSS标准中,Web字体即将迎来一个趋于复杂的崭新时代.下面是一些基本的关于字体的规则,特别适用于Web字体. 原文地 ...
- epoll函数与参数总结学习 & errno的线程安全
select/poll被监视的文件描述符数目非常大时要O(n)效率很低:epoll与旧的 select 和 poll 系统调用完成操作所需 O(n) 不同, epoll能在O(1)时间内完成操作,所以 ...
- C++—函数
一.函数的基本知识 要使用C++函数,必须完成一下工作: (1)提供函数定义: (2)提供函数原型: (3)调用函数. 1.定义函数 可以将函数分为两类,有返回值的函数和没有返回值的函数.没有返回值的 ...
- hiho_1049 二叉树遍历
题目大意 给出一棵二叉树的前序和中序遍历结果,求出后序遍历的结果.保证二叉树中节点值均不相同. 分析 通过前序和中序遍历的结果,我们可以构建出二叉树,若构建出二叉树,则后序遍历的结果很容易求出(当然递 ...