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 ...
 
随机推荐
- Windows7设置局域网文件共享
			
首先要实现共享必须设置共享的机器与访问共享的机器在同一个工作组中. 右键桌面上的计算机图标=>属性 如果不一样的话,就需要点击[更改设置] 右键要共享的文件或者文件夹,点击[共享]打开共享标签: ...
 - postman测试传入json
 - Python3 简单封装 sqlite3 - SimpleToolSql
			
#coding: utf-8 #Author:boxker #Mail:icjb@foxmail.com import sqlite3 import os class simpleToolSql(): ...
 - Linux 命令、配置文件及操作
			
Linux 命令.配置文件及操作 命令 命令 参数 说明 A alias.unalias 命令别名 B C cat 查看文件内容 cd 切换目录 chown 修改拥有着 chgrp 修改所属组 chm ...
 - centos7.4进入单用户模式
			
centos7.4进入单用户模式 1 - 在启动grub菜单,选择编辑选项启动 2 - 按键盘e键,来进入编辑界面 3 - 找到Linux 16的那一行,将ro改为rw init=/sysroot/b ...
 - 万门大学Python零基础10天进阶班视频教程
			
点击了解更多Python课程>>> 万门大学Python零基础10天进阶班视频教程 课程简介: 旨在通过两周的学习,让学生不仅能掌握python编程基础从而进行计算机程序的开发, 还 ...
 - linux的一些权限的操作 chmod
			
u ---属主 g ---用户组 o ---其他组 + ----赋予权限 - ----取消权限 = ----赋予权限并取消以前的权限 r = 4 //读 w =2 //写 x =1 //执 ...
 - leetcode-16-greedyAlgorithm
			
455. Assign Cookies 解题思路: 先将两个数组按升序排序,然后从后往前遍历,当s[j] >= g[i]的时候,就把s[j]分给g[i],i,j都向前移动,count+1;否则向 ...
 - light oj 1104 Birthday Paradox (概率题)
			
Sometimes some mathematical results are hard to believe. One of the common problems is the birthday ...
 - Hadoop4.2HDFS测试报告之二
			
第一组:文件存储写过程记录 测试系统组成 存储类型 测试程序或命令 测试文件大小(Mb) 文件个数(个) 客户端并发数(个) 写速率(M/s) NameNode:1 DataNode:1 本地存储 s ...