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 ...
随机推荐
- CSS基础(一)
一.CSS概述 CSS 指层叠样式表 (Cascading Style Sheets) 样式定义如何显示 HTML 元素 样式通常存储在样式表中 把样式添加到 HTML 4.0 中,是为了解决内容与表 ...
- 【Maven】 (请使用 -source 8 或更高版本以启用 lambda 表达式)
在使用mvn install编译maven项目时,报了 “ (请使用 -source 8 或更高版本以启用 lambda 表达式)”错误,是因为设置的maven默认jdk编译版本太低的问题. 可使用两 ...
- vue-highlightjs的使用小结
万能的github真主,让我们强大!在vue的项目中想使用highlight.js这样的代码高亮?有人帮助我们实现了vue-highlightjs 安装 yarn add highlight.js - ...
- 20181206(re,正则表达式,哈希)
1.re&正则表达式 2.hashlib 一:re模块&正则表达式 正则:正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描 ...
- leetcode-10-basic
35. Search Insert Position Given a sorted array and a target value, return the index if the target i ...
- Linuxx学习-特殊文件与进程
具有 SUID/SGID 权限的指令执行状态 SUID 的权限其实与进程的相关性非常的大!为什么呢?先来看看 SUID 的程序是如何被一般用户 执行,且具有什么特色呢? SUID 权限仅对二进制程序( ...
- VS2017生成.net core项目报错:The current .NET SDK does not support targeting .NET Core 2.1. Either
今天在生成一个项目的时候,生成报错,错误如下:The current .NET SDK does not support targeting .NET Core 2.1. Either target ...
- excludeFromRecents标签
Android:excludeFromRecents控制在不在recent列表中显示. true时不显示:false显示,默认. 运行如下activity后,不会显示在recent列表中. <a ...
- 【Linux】tcpdump命令详解
tcpdump可以将网络中传送的数据包的“头”完全截获下来提供分析. 它支持针对网络层.协议.主机.网络或端口的过滤,并提供and.or.not等逻辑语句帮你过滤到无用的信息. 实用命令实例 1.普通 ...
- 转:Generating PDFs from Web Pages on the Fly with jsPDF
The Portable Document Format has been one the major innovations in the fields of desktop publishing ...