Subsets

Given a set of distinct integers, S, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

For example,
If S = [1,2,3], a solution is:

[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]

解法一:

遍历S.size()位数的所有二进制数,1代表对应位置的元素在集合中,0代表不在。

一共2^n种情况。

详细步骤参照Subsets II

class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
vector<vector<int> > result;
int size = S.size();
for(int i = ; i < pow(2.0, size); i ++)
{//2^size subsets
vector<int> cur;
int tag = i;
for(int j = size-; j >= ; j --)
{//for each subset, the binary presentation has size digits
if(tag% == )
cur.push_back(S[j]);
tag >>= ;
if(!tag)
break;
}
sort(cur.begin(), cur.end());
result.push_back(cur);
}
return result;
}
};

解法二:

遍历所有元素,记当前元素为S[i]

遍历当前所有获得的子集,记为ret[j]

将S[i]加入ret[j],即构成了一个新子集。

详细步骤参照Subsets II

class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
sort(S.begin(), S.end());
vector<vector<int> > ret;
vector<int> empty;
ret.push_back(empty);
for(int i = ; i < S.size(); i ++)
{
int size = ret.size();
for(int j = ; j < size; j ++)
{
vector<int> newset = ret[j];
newset.push_back(S[i]);
ret.push_back(newset);
}
}
return ret;
}
};

【LeetCode】78. Subsets (2 solutions)的更多相关文章

  1. 【LeetCode】78. Subsets 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

  2. 【LeetCode】90. Subsets II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

  3. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  4. 【一天一道LeetCode】#78. Subsets

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 【medium】78. Subsets

    求集合不重复的子集: 下面python的写法很好啊! class Solution(object): def subsets(self, nums): """ :type ...

  6. 【LeetCode】77. Combinations (2 solutions)

    Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ...  ...

  7. 【LeetCode】90.Subsets II

    Subsets II Given a collection of integers that might contain duplicates, nums, return all possible s ...

  8. 【LeetCode】18. 4Sum (2 solutions)

    4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  9. 【LeetCode】46. Permutations (2 solutions)

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

随机推荐

  1. python 基本语法速览,快速入门

    https://zhuanlan.zhihu.com/p/24536868 学习参考于这个博文. 我做一个笔记. 关于python一些常用的语法快速的预览,适合已经掌握一门编程语言的人.零基础,没有任 ...

  2. Python 实现简单的感知机算法

    感知机 随机生成一些点和一条原始直线,然后用感知机算法来生成一条直线进行分类,比较差别 导入包并设定画图尺寸 import numpy as np import matplotlib.pyplot a ...

  3. MP2359 1.2A, 24V, 1.4MHz Step-Down Converter in a TSOT23-6

    The MP2359 is a monolithic step-down switch mode converter with a built-in power MOSFET.It achieves ...

  4. bash中的浮点数处理

        Bash中的变量没有数据类型的定义,这样,在处理字符串和数值时会带来麻烦.例如,使用-eq比较数值,==比较字符串等.另外,Bash中常用的let.expr仅支持整数运算,不支持浮点数计算.要 ...

  5. u-boot中环境变量的实现

    转载:http://blog.chinaunix.net/uid-28236237-id-3867041.html U-boot中通过环境参数保存一些配置,这些配置可以通过修改环境参数.保存环境参数. ...

  6. 【java】弃用System.out.println(),迎接log

    展示性能差异: 1.System.out.println()输出 package com.sxd.swapping.test; import org.junit.Test; public class ...

  7. [Android Pro] Android7.0系统 关于Android获取流量计数TrafficStats.getUidRxBytes(uid)和TrafficStats.getUidTxBytes(uid)返回-1解决方案

    reference : http://blog.csdn.net/zhangyong7112/article/details/54574214 最近一个关于流量的项目在Android7.0系统的手机上 ...

  8. Mac通过wifi连接 Android设备

    公司用的全是mac开发,但是全是type-C接口,每次背电脑回家啊,还得带个数据线转换器…… 想着回来,直接通过Wi-Fi连接手机就好,发现完全忘了之前套路,现在赶紧记下一波,保证包教包会! STEP ...

  9. java springMVC 报400错误问题

    java springMVC 中如果报400错误 很有可能是因为时间转换的问题. 我在项目中就遇到了这个问题,是因为我少引用了一个库,如果是因为时间问题的话添加以下依赖就可以解决. <depen ...

  10. 数学图形(2.18)Hyperbolical conical spiral双曲圆锥螺线

    双曲圆锥螺线 #http://www.mathcurve.com/courbes3d/spiralehyperbolique/spiralehyperbolique.shtml vertices = ...