2014-05-02 02:28

题目链接

原题:

I/P: N, k 

O/P: all subset of N with exactly K elements. 

eg: I/p: N = , K =
O/p:

题目:从N个数中选K个,输出所有选法。

解法:递归解决。

代码:

 // http://www.careercup.com/question?id=6204973461274624
#include <vector>
using namespace std; class Solution {
public:
void findKSubsets(int n, int k, vector<vector<int> > &result) {
if (n <= ) {
return;
}
if (k < || k > n) {
return;
} vector<int> subset; DFS(, , subset, result, n, k);
}
private:
void DFS(int in, int ik, vector<int> &subset,
vector<vector<int> > &result,
const int &n, const int &k) {
if (ik == k) {
result.push_back(subset);
return;
} int i;
for (i = in + ; i <= n - (k - ik) + ; ++i) {
subset.push_back(i);
DFS(i, ik + , subset, result, n, k);
subset.pop_back();
}
};
}; int main()
{
vector<vector<int> > result;
int n, k;
int i, j;
Solution sol; while (scanf("%d%d", &n, &k) == && (n > && k > )) {
sol.findKSubsets(n, k, result);
printf("{\n");
for (i = ; i < (int)result.size(); ++i) {
printf(" {");
for (j = ; j < (int)result[i].size(); ++j) {
printf((j ? ", %d" : "%d"), result[i][j]);
}
printf("}\n");
}
printf("}\n"); for (i = ; i < (int)result.size(); ++i) {
result[i].clear();
}
result.clear();
} return ;
}

Careercup - Facebook面试题 - 6204973461274624的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  8. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  9. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

随机推荐

  1. IntelliJ IDEA 中集成使用git(2015年06月10日)

    前提:需要有一个git账号,https://github.com/ 1.首先需要下载一个Github,https://windows.github.com 安装之后的界面是酱紫的,非常简洁美观 2.在 ...

  2. 公用的stringUtil工具

    (function(){ var ISCHINESE = /[\u4e00-\u9fa5]/; var getData = function( value , maxLenth , isStrick ...

  3. C# Tips: Draw a data table in console

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. 经典算法系列--kmp

    前言 之前对kmp算法虽然了解它的原理,即求出P0···Pi的最大相同前后缀长度k:但是问题在于如何求出这个最大前后缀长度呢?我觉得网上很多帖子都说的不是很清楚,总感觉没有把那层纸戳破,后来翻看算法导 ...

  5. byte[] 清空

    1. using(byte buff = new byte[Size]){  // 你要用的代码,} 2. Array.Clear(bytes, 0 ,bytes.Length);

  6. button等按钮onclientclick事件失效

    如果确定JS没写错 第一种方法: 在JS方法最后return false; 调用方法前加上return 第二种方法: 在JS方法最后event.returnValue=false; 附加:event. ...

  7. Myeclipse安装svn插件(link方式)

    Myeclipse安装svn插件(link方式) 优点:1.离线安装2.非侵入式 演示版本 myeclipse--myeclipse8.6 svn--subeclipse-site-1.6.5.zip ...

  8. Windows下OpenCV的环境配置

    首先去官网下载所需版本的OpenCV(我这里下载的是OpenCV2.4.9),然后安装(也就是解压缩)到某个地方(个人推荐解压到硬盘的根目录).解压完成后,可以得到如下的目录结构(版本不同,可能会有一 ...

  9. Visual Studio 2012 使用免费的Team Foundation Service

    VS2012提供了在线的TFS服务,免费支持五人小团队,收费情况尚未确定,下面本文演示如何申请和连接在线TFS 服务器. 一.申请TFS服务 首先,打开VS2012,看看是否有团队资源管理器,如果没有 ...

  10. .Net Core下如何管理配置文件(转载)

    原文地址:http://www.cnblogs.com/yaozhenfa/p/5408009.html 一.前言 根据该issues来看,System.Configuration在.net core ...