LeetCode OJ——Subsets
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的更多相关文章
- 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] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- 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 ...
随机推荐
- mongo 副本集+分片 配置
服务器规划如下: 副本集名称|服务器IP 192.168.56.111 192.168.56.112 192.168.56.113 shard1 3201 3201 3201 shard2 3202 ...
- mysql 添加数据如果数据存在就更新ON DUPLICATE KEY UPDATE和REPLACE INTO
#下面建立game表,设置name值为唯一索引. CREATE TABLE `game` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar( ...
- C++简易酒店管理系统,实现(查询、入住、退房、楼层选择、退出)功能
#include <iostream> #include <string.h> #include <stdlib.h> void enter(); void che ...
- 反爬虫之搭建IP代理池
反爬虫之搭建IP代理池 听说你又被封 ip 了,你要学会伪装好自己,这次说说伪装你的头部.可惜加了header请求头,加了cookie 还是被限制爬取了.这时就得祭出IP代理池!!! 下面就是requ ...
- BFS、模拟:UVa1589/POJ4001/hdu4121-Xiangqi
Xiangqi Xiangqi is one of the most popular two-player board games in China. The game represents a ba ...
- German Collegiate Programming Contest 2015
// Legacy Code #include <iostream> #include <cstdio> #include <cstring> #include & ...
- ACM-ICPC 2017 Asia Urumqi A. Coins
Alice and Bob are playing a simple game. They line up a row of n identical coins, all with the heads ...
- Linux任务计划、周期性任务执行
Linux任务计划.周期性任务执行 周期性任务执行: cron 守护进程(crond):服务,不间断地运行于后台 # service crond {start|stop|status|restart} ...
- POJ2239二分匹配
开始以为是最长路,想着把每一门课程的每一节课时作为一个点去建有向图...然后写的时候发现点太多了(300*7*12)建图特麻烦,就果断放弃了这个思路. 然后开始用排除法来想用什么算法合适,没环不可能缩 ...
- JAVA-基础(五) 更多工具集
1.StringTokenizer(字符串标记) StringTokenizer实现枚举(Enumeration)接口.因此,给定一个输 入字符串,可以使用StringTokenizer对包含于其中的 ...