【LeetCode】78. Subsets (2 solutions)

时间:2022-09-11 21:20:51

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;
}
};

【LeetCode】78. Subsets (2 solutions)

解法二:

遍历所有元素,记当前元素为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)

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

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

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

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

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

  3. 【LeetCode】90&period; Subsets II &lpar;2 solutions&rpar;

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

  4. 【一天一道LeetCode】&num;78&period; Subsets

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

  5. 【medium】78&period; Subsets

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

  6. 【LeetCode】77&period; Combinations &lpar;2 solutions&rpar;

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

  7. 【LeetCode】90&period;Subsets II

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

  8. 【LeetCode】18&period; 4Sum &lpar;2 solutions&rpar;

    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&period; Permutations &lpar;2 solutions&rpar;

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

随机推荐

  1. iis虚拟目录名称&OpenCurlyDoubleQuote;ReportServer”的巧合

      今天测试一个Crystal Report网站的报表服务,建立一个虚拟目录,名为ReportServer,结果无论怎样访问浏览器都返回 localhost/ReportServer - / Micr ...

  2. day26:面向对象进阶:set、get、del反射和内置

    三 __setattr__,__delattr__,__getattr__ __开头的都是内置的,不定义系统都会有.如果自己定义的话,就会覆盖系统内置的,执行自定义的部分(是否有完成设置的语法,有的话 ...

  3. python爬虫代码

    原创python爬虫代码 主要用到urllib2.BeautifulSoup模块 #encoding=utf-8 import re import requests import urllib2 im ...

  4. Linux文件权限管理

    一.设置文件所属的用户以及所属的组(chown,chgrp) chgrp用来更改文件的组拥有者,其一般格式为:chgrp [option] group file(1)把文件test的组拥有者改为zfs ...

  5. JObject对json的操作

    一,需去程序集添加using Newtonsoft.Json.Linq;引用 using System; using System.Collections.Generic; using System. ...

  6. lucene全文搜索之二:创建索引器(创建IKAnalyzer分词器和索引目录管理)基于lucene5&period;5&period;3

    前言: lucene全文搜索之一中讲解了lucene开发搜索服务的基本结构,本章将会讲解如何创建索引器.管理索引目录和中文分词器的使用. 包括标准分词器,IKAnalyzer分词器以及两种索引目录的创 ...

  7. jmeter每10个停一会实现方案

    foreach控制器中加个if控制器,if控制器条件${__groovy("${__counter(TRUE,)}".toInteger() % 10 == 0,)},再往if控制 ...

  8. string的七种用法

    以下是string的七种用法,注意哦,记得要时常去查看java的API文档,那个里面也有很详细的介绍 1>获取 1.1:字符串中包含的字符数,也就是字符串的长度.  int length():获 ...

  9. CentOS httpd服务(Apache)

    1.从ISO镜像安装,Apache 服务的软件包名称为 httpd #检查源配置[root@localhost media]# cat /etc/yum.repos.d/CentOS-Media.re ...

  10. Cocos2d-x开发---关于安卓打包所遇到的错误记录

         非常久都没有在安卓打过包了.之前的项目因为某些问题没有考虑做安卓版本号,所以涉及到安卓打包的时候都是自己在折腾.      这段时间离职了,空余时间就有非常多了.所以我能够折腾点事了.想起来 ...