2014-03-20 03:08

题目:给定一个集合,返回其幂集。

解法:DFS。

代码:

 // 9.4 Return all subsets of a set
#include <cstdio>
#include <vector>
using namespace std; void getSubsets(const vector<int> &v, int idx, vector<int> &buffer, vector<vector<int> > &res)
{
if (idx == (int)v.size()) {
res.push_back(buffer);
} else {
getSubsets(v, idx + , buffer, res);
buffer.push_back(v[idx]);
getSubsets(v, idx + , buffer, res);
buffer.pop_back();
}
} int main()
{
int i, j;
vector<vector<int> > res;
vector<int> v;
vector<int> buffer;
int n; while (scanf("%d", &n) == && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
scanf("%d", &v[i]);
} getSubsets(v, , buffer, res); for (i = ; i < (int)res.size(); ++i) {
printf("[");
for (j = ; j < (int)res[i].size(); ++j) {
if (j == ) {
printf("%d", res[i][j]);
} else {
printf(" %d", res[i][j]);
}
}
printf("]\n");
res[i].clear();
}
res.clear();
buffer.clear();
v.clear();
} return ;
}

《Cracking the Coding Interview》——第9章:递归和动态规划——题目4的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  5. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目11

    2014-03-21 20:20 题目:给定一个只包含‘0’.‘1’.‘|’.‘&’.‘^’的布尔表达式,和一个期望的结果(0或者1).如果允许你用自由地给这个表达式加括号来控制运算的顺序,问 ...

  9. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目10

    2014-03-20 04:15 题目:你有n个盒子,用这n个盒子堆成一个塔,要求下面的盒子必须在长宽高上都严格大于上面的.如果你不能旋转盒子变换长宽高,这座塔最高能堆多高? 解法:首先将n个盒子按照 ...

  10. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目9

    2014-03-20 04:08 题目:八皇后问题. 解法:DFS解决. 代码: // 9.9 Eight-Queen Problem, need I say more? #include <c ...

随机推荐

  1. 257. Binary Tree Paths (dfs recurive & stack)

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  2. spring-mabatis整合的配置文件

    1.spring.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  3. vue.js 错误提示bash: vue: command not found

    在使用 vue init webpack vue-demo 进行demo的下载时,提示vue: command not found,原因是环境变量没有进行配置,所以会出现这个问题,解决办法 找到你安装 ...

  4. 关于Content-Type: multipart/form-data的支持

    关于Content-Type: multipart/form-data的支持  发布于 537天前  作者 yzhang226  303 次浏览  复制  上一个帖子  下一个帖子  标签: 无 今天 ...

  5. 【[ZJOI2015]诸神眷顾的幻想乡】

    题目 听说这是广义\(SAM\)的板子 看来对于广义\(SAM\)我也就只会板子了 叶子数很少,所以可以枚举每一个叶子节点作为根建一遍\(Trie\)树 只需要对\(Trie\)树建出\(SAM\)就 ...

  6. 富文本 文字图片点击,(TextView)

    textview上的富文本支持 文字,图片的点击事件 - (void)protocolIsSelect:(BOOL)select { NSMutableAttributedString *attrib ...

  7. c#按钮如何避免重复点击后报错

    前言:感谢51·halcon的绝地武士大佬啊,虽然你不认识我,但是我从你那学到了很多知识,真的感谢您对知识的无私传播哈哈(两天一个博客有在坚持的,都是草稿,等这个实习阶段过去了再回来整理博客~) bt ...

  8. Linux 安装Oracle11g完整安装图文教程另附基本操作 (分享)

    一.修改操作系统核心参数 在Root用户下执行以下步骤: 1)修改用户的SHELL的限制,修改/etc/security/limits.conf文件 输入命令:vi /etc/security/lim ...

  9. hdu_2837_Calculation(欧拉函数,快速幂求指数循环节)

    Assume that f(0) = 1 and 0^0=1. f(n) = (n%10)^f(n/10) for all n bigger than zero. Please calculate f ...

  10. poj_1845_Sumdiv

    Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S m ...