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 ...
随机推荐
- DNS 工作原理是什么,域名劫持、域名欺骗、域名污染又是什么
DNS 工作原理是什么,域名劫持.域名欺骗.域名污染又是什么 2014年11月27日 10:05:40 阅读数:6726 标签: dns网络互联网顶级域名递归 更多 个人分类: 网络学习 一.DN ...
- Thinkphp5的安装
很长没有码代码了,现在开始做这件事情的意义已经完全与以前不一样了.因为最近有相当长的一段休息时间,是个学习的好时间啊.之前接触过TP3.2,听说后来的版本有挺大的改动,因此呢,现在终于有时间可以好好的 ...
- 让Web站点崩溃最常见的七大原因
转载自: https://blog.csdn.net/u012981511/article/details/53503242 1. 磁盘已满 导致系统无法正常运行的最可能的原因是磁盘已满.一个好的网络 ...
- 中国电信物联网平台入门学习笔记7:NB-IOT信号如何检测
NB-IOT设备会因为信号的原因,数据发不出.但数据发不出的原因有很多,这么排除是NB-IOT信号的问题呢?那就需要NB-IOT信号检测装置. 网上的信号检测设备 作为一个常年蜗居在实验室的穷屌丝而言 ...
- LeetCode(147) Insertion Sort List
题目 Sort a linked list using insertion sort. 分析 实现链表的插入排序 注意: 程序入口的特殊输入判断处理! 节点的链接处理,避免出现断链! AC代码 /** ...
- Phonegap环境配置和安装插件
一:安装好jdk(配置好环境变量) 二:安装好Android SDK(配置好环境变量path F:\Android\android-sdk-windows\platform-tools;F:\Andr ...
- HDU 3486 Interviewe RMQ
题意: 将\(n\)个数分成\(m\)段相邻区间,每段区间的长度为\(\left \lfloor \frac{n}{m} \right \rfloor\),从每段区间选一个最大值,要让所有的最大值之和 ...
- 《Scrum实战》第1次课课后任务
1.必做任务:从知行角度总结T平台 从知行角度总结T平台 头(知识,学习) 做得好的 宣贯会 引入敏捷思想 敏捷宣言 敏捷原则 质量风险前移原则 引入最佳实践 包括了XP的大部分实践 不足 项目管理框 ...
- 剑指offer算法编程题目部分汇总(解法略)
总结一下本书中遇到的大部分面试题.面试题3:二维数组中的查找 题目:在一个二维数组中,每一行都按照从左到右的递增顺序排列,每一列都按照从上到下递增的顺序排列,请完成一个函数,输入这样的一个整数,判断数 ...
- Selenium WebDriver-判断页面中某一元素是否可操作
driver.get("http://127.0.0.1/test_enable.html") i1=driver.find_element_by_id("input1& ...