Given a set of distinct integers, nums, 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 nums = [1,2,3], a solution is:

[

[3],

[1],

[2],

[1,2,3],

[1,3],

[2,3],

[1,2],

[]

]

这道题能够使用两种方法求解,一是使用位操作,另外是使用深度优先搜索和回溯。可是我仅仅想出了位操作,深度优先的方法是看了Discuss后想出来的。

解法一:位操作

对于数组[1,2,3]。能够用一个下标0和1表示是否选择该数字,0表示未选择。1表示选中。那么每一组3个0和1的组合表示一种选择,3位共同拥有8种选择。各自是:

000 相应[]

001 相应[3]

010 相应[2]

011 相应[2,3]

100 …

101

110

111

那么上面为1的位表示数组中该位被选中。

那么仅仅须要遍历0到1<< length中的数。推断每个数中有那几位为1,为1的那几位即会构成一个子集中的一个元素。

runtime:8ms

class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
int length=nums.size();
sort(nums.begin(),nums.end());
vector<vector<int> > result;
for(int i=0;i<1<<length;i++)
{
vector<int> tmp;
//计算i中有那几位为1
for(int j=0;j<length;j++)
{
//推断i中第j位是否为1
if(i&1<<j)
{
tmp.push_back(nums[j]);
}
}
result.push_back(tmp);
}
return result;
} };

解法二:回溯法

还能够使用深度优先搜索来遍历数组,採用回溯法来剔除元素。使用一个变量来记录路径。每遍历到一个元素即表示找到一条路径,将其增加子集中。

对于数组[1,2,3]

从1開始递归查询2,3,对于2,继续向下搜索。搜索完后将2删除。

runtime:8ms

class Solution {
public:
//使用深度优先的回溯法
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> result;
vector<int> path;
sort(nums.begin(),nums.end());
result.push_back(path);
dfs(nums,0,path,result);
return result;
}
void dfs(vector<int>& nums,int pos,vector<int> & path,vector<vector<int>> & result)
{
if(pos==nums.size())
return; for(int i=pos;i<nums.size();i++)
{
path.push_back(nums[i]);
result.push_back(path);
dfs(nums,i+1,path,result);
path.pop_back();
}
} };

LeetCode78:Subsets的更多相关文章

  1. Leetcode78. Subsets子集

    给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3],   [1],   [2 ...

  2. leetcode 78,236,300

    ---恢复内容开始--- 2018.3.16目前已刷27题,打卡记录有意思的题目. leetcode78 subsets 思路1:DFS遍历子集,每遇到一个数就把该数加上原来的子集变成新的子集. cl ...

  3. [Swift]LeetCode78. 子集 | Subsets

    Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...

  4. Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  5. Subsets

    Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...

  6. [LeetCode] Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  7. [LeetCode] Subsets 子集合

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  8. LeetCode:Subsets I II

    求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...

  9. 【leetcode】Subsets II (middle) ☆

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

随机推荐

  1. android开发之自定义圆形ImagView

    在日常使用中我们经常会使用到圆形的图片,但是android系统中并没有默认的圆形控件,所以我们需要自己来写一个自定义的ImagView来显示一张圆形的图片,下面先看效果 详细的方法是我们自定义一个类, ...

  2. C# 委托、事件,lamda表达式

    参考文章 1. 委托Delegate C#中的Delegate对应于C中的指针,但是又有所不同C中的指针既可以指向方法,又可以指向变量,并且可以进行类型转换, C中的指针实际上就是内存地址变量,他是可 ...

  3. 今天在CSDN看懂这个帖子,也是我的困惑,记录一下(过了三十的码农,你选择的是哪个,说出你的想法)

    http://bbs.csdn.net/topics/390944177 1.继续开发生涯,做资深码农,从senior.team lead.tech lead到principal,如果你无欲无求,可以 ...

  4. UVA 1665 Islands

    题意:输入一个n*m矩阵,每一个格子都有一个正整数,再输入T个整数ti,对于每一个ti,输出大于ti的正整数组成多少个四连快 思路:正着做的话事实上相当于删除连通块,而假设反着做的话就相当于变成添加连 ...

  5. iOS 执行时

    一.什么是执行时(Runtime)? 执行时是苹果提供的纯C语言的开发库(执行时是开发中经经常使用到的底层技术) 二.执行时的作用? 能获得某个类的全部成员变量 能获得某个类的全部属性 能获得某个类的 ...

  6. 使用Spring boot整合Hive,在启动Spring boot项目时,报错

    使用Spring boot整合Hive,在启动Spring boot项目时,报出异常: java.lang.NoSuchMethodError: org.eclipse.jetty.servlet.S ...

  7. devops流程

    学习资源: https://www.youtube.com/watch?v=JBtWxj9l7zM&list=PLoYCgNOIyGAAzevEST2qm2Xbe3aeLFvLc&t= ...

  8. elasticsearch 基础性操作

    1 基础概念 Elasticsearch是一个近实时的系统,从你写入数据到数据可以被检索到,一般会有1秒钟的延时.Elasticsearch是基于Lucene的,Lucene的读写是两个分开的句柄,往 ...

  9. ListView控件绑定DataSet

    DataSet数据集,数据缓存在客户端内存中,支持断开式连接.   在对DataSet做操作的时候,首先一定要修改其行的状态,然后执行SqlDataAdapter的Update方法,Update方法根 ...

  10. android项目 之 记事本(13) ----- 查看图片及播放录音

    本文是自己学习所做笔记,欢迎转载.但请注明出处:http://blog.csdn.net/jesson20121020 今天就来实现下查看图片及录音的功能,在编辑或者浏览记事时,点击图片.打开一个自己 ...