https://oj.leetcode.com/problems/combinations/

给一个集合,求个数为k的所有子集

递归调用,深搜

 class Solution {
public:
vector<vector<int> > combine(int n, int k){
vector<vector<int> > ans;
if(n== || n<k)
return ans; vector<int> ansPiece; if(n==k)
{
for(int i = ;i<=n;i++)
{
ansPiece.push_back(i);
}
ans.push_back(ansPiece);
return ans;
} combination2(ans,k,,ansPiece,n); return ans;
}
void combination2(vector<vector<int> > &ans,int targetLen,int pivot,vector<int> &ansPiece,int n)
{
if(targetLen == ansPiece.size())
{
ans.push_back(ansPiece);
return;
}
if(pivot > n)
return; combination2( ans,targetLen,pivot+,ansPiece,n); ansPiece.push_back(pivot);
combination2( ans,targetLen,pivot+,ansPiece,n);
ansPiece.pop_back();
}
};

LeetCode OJ--Combinations *的更多相关文章

  1. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  2. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  3. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  4. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  5. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  7. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  8. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  10. LeetCode OJ 77. Combinations

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

随机推荐

  1. 查询集 QuerySet和管理器Manager

    查询集 QuerySet 查询集,也称查询结果集.QuerySet,表示从数据库中获取的对象集合. 当调用如下过滤器方法时,Django会返回查询集(而不是简单的列表): all():返回所有数据. ...

  2. pandas知识点(汇总和计算描述统计)

    调用DataFrame的sum方法会返还一个含有列的Series: In [5]: df = DataFrame([[1.4,np.nan],[7.1,-4.5],[np.nan,np.nan],[0 ...

  3. Diycode开源项目 BaseApplication分析+LeakCanary第三方+CrashHandler自定义异常处理

    1.BaseApplication整个应用的开始 1.1.看一下代码 /* * Copyright 2017 GcsSloop * * Licensed under the Apache Licens ...

  4. Linux下 导入导出数据库

    1.怎么找到mysql下的bin? 1.1首先我在网上百度了一些Linux下如何导入导出数据.基本都是用mysqldump命令,但是如果没有到指定目录 下,执行这个命令是没有用的.一开始我还以为要在m ...

  5. HDU 3848 CC On The Tree 树形DP

    题意: 给出一棵边带权的树,求距离最近的一对叶子. 分析: 通过DFS计算出\(min(u)\):以\(u\)为根的子树中最近叶子到\(u\)的距离. 然后维护一个前面子树\(v_i\)中叶子到\(u ...

  6. Redis数据结构以及Strings型操作

    Redis数据结构图: Strings型   <String key,String value>: keys * 查看所有key get 获取key的value值 append 向key对 ...

  7. windows下,cmd 运行 python 脚本,选中文字就停止运行了【已解决】

    参考资料: https://jingyan.baidu.com/article/ce09321bb95dda2bff858f26.html 问题原因: cmd 里面,快速编辑模式会暂停程序 解决步骤: ...

  8. 设计模式之第20章-访问者模式(Java实现)

    设计模式之第20章-访问者模式(Java实现) “嘿,你脸好红啊.”“精神焕发.”“怎么又黄了?”“怕冷,涂的,涂的,蜡.”“身上还有酒味,露馅了吧,原来是喝酒喝的啊.”“嘿嘿,让,让你发现了,今天来 ...

  9. MySql数据库 - 1.安装

    下载: 官网:www.mysql.com 打开官网之后依次点击:DOWNLOADS - Windows - MySql Installer MySql Installer 包含的功能,使用C#连接数据 ...

  10. 【bzoj2186】[Sdoi2008]沙拉公主的困惑 欧拉函数

    题目描述 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大户沙拉公主决定预测一下大富翁国现在所有真钞票的 ...