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. Python学习笔记(二):数据类型

    一.python中的数据类型 python中的数据类型包括:整型.浮点型.布尔型.字符串类型 整型(int)和浮点型(float) Python中的整型只有int,没有short.long:浮点型fl ...

  2. North American Invitational Programming Contest (NAIPC) 2017

    (待补) A. Pieces of Parentheses 将括号处理完成后排序,方式参加下面的博客.然后做一遍背包即可. 2018 Multi-University Training Contest ...

  3. [BZOJ1503]郁闷的出纳员(Splay)

    Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常 ...

  4. Poj3061Subsequence

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  5. kafka 的offset的重置

    最近在spark读取kafka消息时,每次读取都会从kafka最新的offset读取.但是如果数据丢失,如果在使用Kafka来分发消息,在数据处理的过程中可能会出现处理程序出异常或者是其它的错误,会造 ...

  6. 查询语句为“%string_”的情况

    select * from t_user where user_name like '%Joe_%'实际查询出来的语句为: 而不像预计的前两条.

  7. Jquery查询分析器

    find() 方法获得当前元素集合中每个元素的后代,通过选择器.jQuery 对象或元素来筛选.$(this).find("ul[index=1] div input:radio:check ...

  8. PostgreSql基础命令及问题总结

    本章内容: 1.基本命令 基本命令 1.psql -U cdnetworks_beian -d cdnetworks_beian         #-U指定用户,-d指定数据库 2.\l        ...

  9. laravel5.2总结--软删除

    当模型被软删除时,它们并不会真的从数据库中被移除.而是会在模型上设置一个 deleted_at 属性并将其添加到数据库.如果对应模型被软删除,则deleted_at字段的值为删除时间,否则该值为空. ...

  10. Eclipse启动错误:A Java Runtime Environment(JRE) or Java Development Kit(JDK) must be available……

    确保Jdk,Jre都安装完成并且环境变量配置无误的情况下,自动Ecplise报错如下: A Java Runtime Environment (JRE) or Java Development Kit ...