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]的更多相关文章

  1. Combinations ——LeetCode

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  2. Combinations leetcode java

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...

  3. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  4. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  5. [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 ...

  6. [LeetCode] Combinations 组合项

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  7. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  8. LeetCode Factor Combinations

    原题链接在这里:https://leetcode.com/problems/factor-combinations/ 题目: Numbers can be regarded as product of ...

  9. [leetcode 17]Letter Combinations of a Phone Number

    1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...

随机推荐

  1. Keepalive

    https://en.wikipedia.org/wiki/Keepalive Description A keepalive signal is often sent at predefined i ...

  2. 【原创】pads2007 Layout 电气连接性检查过孔显示错误

    如图所示的电源铜皮过孔,勾选stiching选项,连接性检查会报错误: 去掉stiching选项,连接错误消失.

  3. 【转】Haproxy安装及配置

    1.安装 # wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz # tar zcvf haproxy-1.3.20.t ...

  4. Mybatis+struts2+spring整合

    把student项目改造成ssm  struts2 +mybatis+spring 1,先添加spring支持:类库三个,applicationContext.xml写在webinf下四个命名空间,监 ...

  5. Field+offset(len)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. jquery 跳转到当前页面指定位置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. oracle linux 下卸载

    1. 关闭数据库 shutdown immeidate 2. 停止 Listener lsnrctl stop 3. 停止http服务(可选) service httpd stop 4. 用su或者重 ...

  8. Java中的JDBC数据库连接

    JDBC编程步骤 1.加载数据库驱动. // 加载驱动 Class.forName(driverClass) // 加载mysql驱动 Class.forName("com.mysql.jd ...

  9. 在Spark上用Scala实验梯度下降算法

    首先参考的是这篇文章:http://blog.csdn.net/sadfasdgaaaasdfa/article/details/45970185 但是其中的函数太老了.所以要改.另外出发点是我自己的 ...

  10. volley超时和重复请求问题

    原文:  Android Volley double post when have slow request I have a problem with Volley POST request on ...