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 ...
随机推荐
- Java中创建(实例化)对象的五种方式
Java中创建(实例化)对象的五种方式1.用new语句创建对象,这是最常见的创建对象的方法. 2.通过工厂方法返回对象,如:String str = String.valueOf(23); 3.运用反 ...
- Ubuntu下Python无法识别中文
在NLP的相关任务中,应用python处理中文是很常见的.在这个过程中,由于编码方式的不一致,可能会出现以下两种错误: 1)SyntaxError: Non-ASCII character in f ...
- CCPC_1003
这个题可以暴力的哟,直接暴力的哟 不用做什么订立的哟 不需要特别判断的哟 去死吧!!!愚蠢的我! #include<bits/stdc++.h> using namespace std; ...
- 安装Mysql community server遇到计算机中丢失msvcr120.dll
一.下载community server版本 Mysql community server版本:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7 ...
- 2 - JVM随笔分类(JVM堆的内存回收)
JVM常用的回收算法是: 标记/清除算法 标记/复制算法 标记/整理算法 其中上诉三种算法都先具备,标记阶段,通过标记阶段,得到当前存活的对象,然后再将非标记的对象进行清除,而对象内存中对象的标记过程 ...
- java并发之(4):Semaphore信号量、CounDownLatch计数锁存器和CyclicBarrier循环栅栏
简介 java.util.concurrent包是Java 5的一个重大改进,java.util.concurrent包提供了多种线程间同步和通信的机制,比如Executors, Queues, Ti ...
- php伪随机数漏洞 以及脚本php_mt_seed的使用教程
前几天在群里看到了一个题目,发现自己没有接触过这个伪随机数这个漏洞,在此记录下. 搜索这两个函数 mt_scrand() mt_rand() mt_scrand(seed)这个函数的意思,是通过分发s ...
- git+jenkins持续集成二-jenkins定时构建语法:定时构建语法
构建位置:选择或创建工程_设置_构建触发器 1. 定时构建语法:* * * * * (五颗星,多个时间点,中间用逗号隔开)第一个*表示分钟,取值0~59第二个*表示小时,取值0~23第三个*表示一个月 ...
- Mac OS X 上的Apache配置
Mac系统自带apache服务器 查看apache版本 sudo apachectl -v 启动apache sudo apachectl start 重启apache sudo apachectl ...
- Android数据储存之SharedPreferences
Android中SharedPreferences通常与Editor连用 接口SharedPreferences常用方法: boolean contains(String str):判断SharedP ...