【leetcode】Subsets (Medium) ☆
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) ☆的更多相关文章
- 【leetcode】Subsets II (middle) ☆
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【leetcode】Subsets
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- 【LeetCode】 Subsets
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- 【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 ...
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
随机推荐
- poj3070 (斐波那契,矩阵快速幂)
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9630 Accepted: 6839 Descrip ...
- Hadoop 之Impala
impala 是基于hive的大数据实时分析查询引擎,直接使用Hive的元数据库metadata意味着impala元数据都存储在hive的metadstore中并且impala兼容hive的 sql解 ...
- js面试题
前几天在学习js的时候,碰到了这样一道面试题,要求计算出给你一个随机乱敲的一个字符串,要求在其中找出那个字符出现的次数最多,以及出现的个数. 这你有两种方案,请大家仔细阅读,有可能在你将来的面试中会碰 ...
- ubutu之qq安装
1.压缩包下载链接 http://pan.baidu.com/s/1nvlgsGh 2.安装教程(引用自百度经验) 如何在Ubuntu中安装QQ
- java之BASE64加解密
1.简介 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,采用Base64编码具有不可读性,即所编码的数据不会被人用肉眼所直接看到. 注:位于jdk的java.util包中. 2. ...
- PHP Socket实现websocket(四)Select函数
int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval *timeout); /* ...
- Lvs原理及部署之ARP协议
1.什么使ARP协议 ARP协议,全称"Address Resolution Protocol" ,中文名是地址解析协议,使用ARP协议可实现通过IP地址获得对应的物理地址(MAC ...
- java项目导入IntelliJ IDEA
(0)之所以有第0步,是因为第一次倒入失败,所以从删除上次倒入的数据开始- 开始删除数据. 启动Intelli J,点击右键删除上次的导入的项目 把配置拷贝到.m2文件夹下,并且删除上次下载的一些依赖 ...
- 网络之AFNetsorking
AFNetsorking作为功能全面的网络第三方,既通俗好用又与时俱进-及时的更新使用了NSURLSession,不得不爱. AFNetsorking使用: 1,AFNetsorking GET请求 ...
- STL标准模板库介绍
1. STL介绍 标准模板库STL是当今每个从事C++编程的人需要掌握的技术,所有很有必要总结下 本文将介绍STL并探讨它的三个主要概念:容器.迭代器.算法. STL的最大特点就是: 数据结构和算法的 ...