http://oj.leetcode.com/problems/subsets/

计算一个集合的子集,使用vector<vector<int> >,使用了进制的思想。

#include<algorithm>
#include<vector>
#include<iostream>
#include<cmath>
using namespace std; class Solution {
private:
void myoutput(vector<int> &input)
{
for(int i=0;i<input.size();i++)
cout<<input[i]<<" ";
}
int mypow(int i)
{
if(i ==0 )
return 1;
int ret = 1;
while(i--)
ret *= 2;
return ret;
} public:
vector<vector<int> > subsets(vector<int> &S) {
// Note: The Solution object is instantiated only once and is reused by each test case. sort(S.begin(),S.end());
//myoutput(S);
int sum = mypow(S.size());
//cout<<sum<<endl;
//cout<<sum<<"sum"<<endl; vector<vector<int> > result;
result.clear(); sum--; vector<int> tep_result;
for(int i = sum;i>0;i--)
{
tep_result.clear();
int _sum = i;
int digit = 0;
while(_sum!=0)
{ if(_sum%2==1)
tep_result.push_back(S[digit]);
digit++;
_sum /= 2;
}
result.push_back(tep_result);
//二维vector的使用方式,是直接添加一维的 }
tep_result.clear();
result.push_back(tep_result);
//for(int j = 0;j<result.size();j++)
//{
//cout<<"this:";
//myoutput(result[j]);
//cout<<endl;
//} return result;
}
}; #include<iostream>
#include"solu.h"
using namespace std; int main()
{
Solution *so = new Solution;
vector<int> input;
input.clear();
/*input.push_back(3);
input.push_back(1);
input.push_back(2);
input.push_back(5);*/
so->subsets(input); //cout<<"ok"<<endl;
if(so!=NULL)
delete so;
so = NULL;
return 0;
}

  

LeetCode OJ——Subsets的更多相关文章

  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] 90.Subsets II tag: backtracking

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  8. [leetcode]90. Subsets II数组子集(有重)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  9. 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 ...

随机推荐

  1. 【思维题 集合hash 树上差分】11.5撸树

    要注重问题的转化和一些结论的推断 题目描述 要致富,先撸树. 一棵树的形状可以简化为一张 $N$ 个点 $M$ 条边的图,由于装备条件限制,你只有撸两次,也就是删去两条边,当这张图不联通时,就意味着树 ...

  2. 【贪心 哈夫曼树】bzoj2923: [Poi1998]The lightest language

    失去了以前用STL乱搞的能力…… 题目描述 语言也是数学上经常研究的一种数据. 给出数学上关于语言的如下定义: 字母表:大小为 K 的字母表是一个由 K 不同的字符组成的集合. 单词:长度为 m 的单 ...

  3. gulp的安装和使用

    安装nodejs -> 全局安装gulp -> 项目安装gulp以及gulp插件 -> 配置gulpfile.js -> 运行任务 1.去nodejs官网安装nodejs 2. ...

  4. python并发编程之进程1(守护进程,进程锁,进程队列)

    进程的其他方法 P = Process(target=f,) P.Pid 查看进程号  查看进程的名字p.name P.is_alive()  返回一个true或者False P.terminate( ...

  5. 跟踪路由 tracert

    由于最近遇到网络出现故障的问题,便使用到Tracert来确定了下出现故障的网络节点 记录下tracert命令相关内容 1. 简介 2. Tracert工作原理... 3. 常用参数 4. 使用示例与输 ...

  6. Linux之crond 服务介绍

    在介绍crond之前,先科普一下什么是进程?什么是线程?什么是程序? 程序:程序是一组指令及参数的集合,指令按照既定的逻辑控制计算机运行.进程则是运行着的程序,是操作系统执行的基本单位.线程则是为了节 ...

  7. Ubuntu首次登入与在线求助man page总结

    1.为了避免瞬间断电造成的Linux系统的危害,建议做为服务器的Linux主机应该加上不断电系统来持续提供稳定的电力 2.默认的图形模式登入中,可以选择语系以及作业阶段.作业阶段为多种窗口管理员软件所 ...

  8. Ubuntu下安装anaconda和pycharm

    折腾了一上午,终于装好了,如下:Python环境的安装: 安装anaconda 建议去https://www.anaconda.com/download/#linux直接用Ubuntu界面的搜狐浏览器 ...

  9. luogu3808 luogu3796 AC自动机(简单版) AC自动机(加强版)

    纪念一下我一晚上写了八遍AC自动机 这是加强版的: #include <iostream> #include <cstring> #include <cstdio> ...

  10. 并查集 - BZOJ 1104 [POI2007]洪水

    BZOJ 1104 [POI2007]洪水 描述 AKD 市处在一个四面环山的谷地里.最近一场大暴雨引发了洪水,AKD 市全被水淹没了.Blue Mary,AKD 市的市长,召集了他的所有顾问(包括你 ...