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.

就是找出数字的全部子集。

我的思路:

设输入是 1  2  3  4

先把输入从小到大排序

长度 0 : []

长度 1 : 把输入数据从第一个数开始 1, 找上一个数字结果中开始数字比1大的解 没有 直接压入 [1]

2,找上一个数字结果中开始数字比2大的解 没有 直接压入 [2]

...

得到长度为1的解是 [1] [2] [3] [4]

长度 2 : 把输入数据从第一个数开始 1, 找上一个数字结果中开始数字比1大的解 有[2] [3] [4], 压入[1 2][1 3][1 4]

2, 找上一个数字结果中开始数字比2大的解 有 [3] [4], 压入[2 3][2 4]

...

得到长度为2的解是 [1 2][1 3][1 4][2 3][2 4][3 4]

长度 3 : 把输入数据从第一个数开始 1, 找上一个数字结果中开始数字比1大的解 有[2 3][2 4][3 4], 压入[1 2 3][1 2 4][1 3 4]

2, 找上一个数字结果中开始数字比2大的解 有 [3 4], 压入[2 3 4]

得到长度为3的解是 [1 2 3][1 2 4][1 3 4][2 3 4]

长度 4 : 把输入数据从第一个数开始 1, 找上一个数字结果中开始数字比1大的解 有[2 3 4], 压入[1 2 3 4]

2, 找上一个数字结果中开始数字比2大的解 没有

得到长度为4的解是 [1 2 3 4]

代码用了posfirst,poslast来表示上一个长度答案的范围。

class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
sort(S.begin(), S.end());
vector<vector<int>> ans;
vector<int> partans;
ans.push_back(partans); //空的 int posfirst = ; //上一个长度子集在ans中的起始下标
int poslast = ; //上一个长度子集在ans中的结束下标 for(int len = ; len <= S.size(); len++) //对长度循环
{
poslast = ans.size() - ;
for(int i = ; i < S.size(); i++) //对起始数字循环
{
while(!ans[posfirst].empty() && S[i] >= ans[posfirst][] && posfirst <= poslast) //跳过上一个长度答案中起始数字小于等于当前起始数字的解
{
posfirst++;
}
for(int pos = posfirst;pos <= poslast; pos++) //获取当前的答案
{
partans.push_back(S[i]); //压入当前数字
for(int j = ; j < ans[pos].size(); j++) //压入上一个长度答案中的数字
{
partans.push_back(ans[pos][j]);
}
ans.push_back(partans);
partans.clear();
}
}
posfirst = poslast + ;
}
return ans;
}
};

大神的解法:https://oj.leetcode.com/discuss/9213/my-solution-using-bit-manipulation

class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
sort (S.begin(), S.end());
int elem_num = S.size();
int subset_num = pow (, elem_num);
vector<vector<int> > subset_set (subset_num, vector<int>());
for (int i = ; i < elem_num; i++)
for (int j = ; j < subset_num; j++)
if ((j >> i) & )
subset_set[j].push_back (S[i]);
return subset_set;
}
};

解释如下:

 This is an amazing solution.Learnt a lot.Let me try to explain this to those who didn't get the logic.

 Number of subsets for { ,  ,  } = ^ .
why ?
case possible outcomes for the set of subsets
-> Take or dont take =
-> Take or dont take =
-> Take or dont take = therefore , total = ** = ^ = { { } , {} , {} , {} , {,} , {,} , {,} , {,,} } Lets assign bits to each outcome -> First bit to , Second bit to and third bit to
Take =
Dont take = ) -> Dont take , Dont take , Dont take = { }
) -> Dont take , Dont take , take = { }
) -> Dont take , take , Dont take = { }
) -> Dont take , take , take = { , }
) -> take , Dont take , Dont take = { }
) -> take , Dont take , take = { , }
) -> take , take , Dont take = { , }
) -> take , take , take = { , , } In the above logic ,Insert S[i] only if (j>>i)& ==true { j E { ,,,,,,, } i = ith element in the input array } element is inserted only into those places where 1st bit of j is
if( j >> & ) ==> for above above eg. this is true for sl.no.( j )= , , , element is inserted only into those places where 2nd bit of j is
if( j >> & ) == for above above eg. this is true for sl.no.( j ) = , , , element is inserted only into those places where 3rd bit of j is
if( j >> & ) == for above above eg. this is true for sl.no.( j ) = , , , Time complexity : O(n*^n) , for every input element loop traverses the whole solution set length i.e. ^n

【leetcode】Subsets (Medium) ☆的更多相关文章

  1. 【leetcode】Subsets II (middle) ☆

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

  2. 【leetcode】Subsets II

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

  3. 【leetcode】Subsets

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

  4. 【LeetCode】 Subsets

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

  5. 【leetcode】3Sum (medium)

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

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

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

  7. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  8. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  9. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

随机推荐

  1. [译]git reflog

    用法 git reflog 显示整个本地仓储的commit, 包括所有branch的commit, 甚至包括已经撤销的commit, 只要HEAD发生了变化, 就会在reflog里面看得到. git ...

  2. 图解Tomcat类加载机制

    说到本篇的tomcat类加载机制,不得不说翻译学习tomcat的初衷. 之前实习的时候学习javaMelody的源码,但是它是一个Maven的项目,与我们自己的web项目整合后无法直接断点调试.后来同 ...

  3. VTK初学一,a Mesh from vtkImageData—球冠

    #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...

  4. ML_R kNN

    邻近算法 K最近邻(kNN,k-NearestNeighbor)分类算法是数据挖掘分类技术中最简单的方法之一.所谓K最近邻,就是k个最近的邻居的意思,说的是每个样本都可以用它最接近的k个邻居来代表. ...

  5. 2015Summer Training #2

    暑假集训昨天刚开始,14级的小伙伴快到齐了,hhhhh ,毕竟下学期区域赛,对我们来说还是很困难的. 打算每天写份总结. UVA 11300 C.Spreading the Wealth 题目大意:n ...

  6. Mac 用户组:staff、 wheel、admin 的区别

    所有的用户都属于 staff 组, 只有具有管理员性质的用户位于 wheel 组中. wheel 是一个特殊的用户组,该组的用户可以使用 su 切换到 root,而 staff 组是所有普通用户的集合 ...

  7. android Activity基类通用方法

    public class BaseActivity extends Activity { Resources res; // 通用资源缩写 @Override protected void onCre ...

  8. Effective Java 读书笔记之七 通用程序设计

    一.将局部变量的作用域最小化 1.在第一次使用变量的地方声明 2.几乎每个变量的声明都应该包含一个初始化表达式:try-catch语句是一个例外 3.使方法小而集中是一个好的策略 二.for-each ...

  9. Unity3D绑定button监听事件

    一.可视化创建及事件绑定 第一步:通过Hierarchy面板创建button,如图 第二步:创建一个脚本名为TestClick,并定义一个名为Click的public方法 ? 1 2 3 4 5 6 ...

  10. Unity调用Android方法

    步骤 废话不多说,直接来步骤吧1.创建工程,弄大概像这样一个界面2.在unity中写好代码,像这样,记得给界面绑定好事件啥的 using UnityEngine; using UnityEngine. ...