LeetCode OJ--Combinations *
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 *的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- 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 ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode OJ 77. Combinations
题目 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exa ...
随机推荐
- 用描述符实现缓存功能和property实现原理
class Lazyproperty: def __init__(self, func): self.func = func def __get__(self, instance, owner): p ...
- TI C64X+通用库函数使用手册
在使用前,当知悉以下几点: 函数进程由手动汇编而成,已充分发挥器件效率.同时TI对外提供C和线性汇编代码 对于个人一些特殊应用,DSPLIB可能会带来额外的cycle消耗 TI DSPLIB依平台和时 ...
- 学习BootStrap3.3.4——敲完全局CSS样式
历时7小时- -(算上晚饭)终于敲完BootStrap CSS样式部分.还是第一次这么持久的敲纯前端,连JS都没有. 正好趁这机会熟悉了Sublime,主要是各个快捷键的用法.目前用到最多的: 而且s ...
- James Bach Rapid Test的感受
前阶段拜读过James大神的快速测试,英文水平有限,阅读起来有点吃力,虽然想亲自参加大神的培训,一直没有机会,不过阿里牛人参加大神的培训,并总结的不错,现在谈谈自己的感想和看法. 进入测试行业不少年了 ...
- MySQL之索引(三)
聚簇索引 聚簇索引并不是一种单独的索引类型,而是一种数据存储方式.具体的细节依赖于其实现方式,但InnoDB的聚簇索引实际上在同一个结构中保存了B-Tree索引和数据行.当表有聚簇索引时,它的数据行实 ...
- 线段树[To be continued]
目录 数据结构--线段树 一.定义 二.性质 三.基本操作 0.结构体 1.建树 2.单点查询 3.单点修改 4.区间修改 5.区间查询 四.题目 单点修改.区域查询模板 五.鸣谢 学姐的Blog 百 ...
- 基础_String
String str1="hello"; String str2="hello"; String str3="hello"; String ...
- Migrate a Domain-based Namespace to Windows Server 2008 Mode
TechNet Library Scripting with Windows PowerShell Windows and Windows Server Automation with Windows ...
- 转:vc与界面开发之间的文章
[很好的一篇文章,很喜欢看同行的心路历程:http://www.vckbase.com/index.php/nv/444.html] 本屌丝在新春放假期间闲来无事,在各大编程论坛溜达了一圈.发现年前的 ...
- bat 处理adb脚本
@echo off REM Funtion: 测试parsermode 接口CdxParserGetMediaInfo 和CdxParserRead REM Code by lzp 2017-05-0 ...