LeetCode78:Subsets
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的更多相关文章
- Leetcode78. Subsets子集
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3], [1], [2 ...
- leetcode 78,236,300
---恢复内容开始--- 2018.3.16目前已刷27题,打卡记录有意思的题目. leetcode78 subsets 思路1:DFS遍历子集,每遇到一个数就把该数加上原来的子集变成新的子集. cl ...
- [Swift]LeetCode78. 子集 | Subsets
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...
- Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- Subsets
Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...
- [LeetCode] Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [LeetCode] Subsets 子集合
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- LeetCode:Subsets I II
求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...
- 【leetcode】Subsets II (middle) ☆
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
随机推荐
- 洛谷 P4538 收集邮票
题目描述 有n种不同的邮票,皮皮想收集所有种类的邮票.唯一的收集方法是到同学凡凡那里购买,每次只能买一张,并且买到的邮票究竟是n种邮票中的哪一种是等概率的,概率均为1/n.但是由于凡凡也很喜欢邮票,所 ...
- php升级版本
Centos下Yum安装PHP5.5,5.6,7.0 默认的版本太低了,手动安装有一些麻烦,想采用Yum安装的可以使用下面的方案: 1.检查当前安装的PHP包 yum list installed | ...
- 随机取若干条记录的SQL语句
原文:随机取若干条记录的SQL语句 MySql中随机提取数据库N条记录 select * from TableName order by rand() limit N SQLServer中随机提取 ...
- linux的file指令
显示文件的类型,用命令 file 可以使你知道某个文件究竟是ELF格式的可执行文件, 还是shell script文 件或是其他的什么格式 例如:#file startx 语 法:file [-beL ...
- /etc/fstab 官方文档
1什么是fstab 2fstab文件示例 3fstab 文件组成 4文件系统标识 4.1Kernel naming 4.2UUID 4.3Label 5建议 5.1atime 参数 5.2tmpfs ...
- selenium _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0
在关闭driver时,如果用close,而不是用quit,会出现如下错误: Exception ignored in: <bound method Popen.__del__ of <su ...
- java 通过流的方式读取本地图片并显示在jsp 页面上(类型以jpg、png等结尾的图片)
Java代码: File filePic = new File(path+"1-ab1.png"); if(filePic.exists()){ FileInputStream i ...
- JAVA Eclipse如何重新设置工作空间workspace
窗口-首选项-常规-启动和关闭,勾选启动时提示工作空间,然后移除现有的工作空间,最好也勾选启动时刷新工作空间 重启之后就可以设置工作空间了
- POJ 2388:Who's in the Middle
Who's in the Middle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31015 Accepted: 1 ...
- autoRelease
cocos2dx采用的是引用计数的方式来管理对象的持有和释放. 所谓引用计数就是说,每个对象都会有一个属性用来记录当前被几个地方引用了.在释放内存的时候会根据这个引用计数来确定是否要用delete操作 ...