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: ...
随机推荐
- sqlldr control file compare with HEX value
when field= UTL_I18N.RAW_TO_CHAR ('e38080', 'AL32UTF8')
- 2017 China Collegiate Programming Contest Final (CCPC 2017)
题解右转队伍wiki https://acm.ecnu.edu.cn/wiki/index.php?title=2017_China_Collegiate_Programming_Contest_Fi ...
- 级联关系(内容大部分来自JavaEE轻量型解决方案其余的是我的想法)
1. 级联关系 在Hibernate程序中持久化的对象之间会通过关联关系互相引用.对象进行保存.更新和删除等操作时,有时需要被关联的对象也执行相应的操作,如:假设需要关联关系的主动方对象执行操作时,被 ...
- log4j教程 5、示例程序
前面我们已经看到了如何创建一个配置文件.本教程将讲解如何生成调试信息和日志在一个简单的文本文件. 下面是我们的例子中创建了一个简单的配置文件.这里再重复一次: 下载最新的Log4j库:http://l ...
- 2017.6.30 用shiro实现并发登录人数控制(实际项目中的实现)
之前的学习总结:http://www.cnblogs.com/lyh421/p/6698871.html 1.kickout功能描述 如果将配置文件中的kickout设置为true,则在另处再次登录时 ...
- Android 比ListView更好用强大的RecyclerView库:RecyclerViewLibrary
RecyclerViewLibrary A RecyclerView libirary ,has some support, like headerAdapter/TreeAdapter,and Pu ...
- [PWA] Add Push Notifications to a PWA with React in Chrome and on Android
On Android and in Chrome (but not on iOS), it's possible to send push notifications with a PWA. We'l ...
- Weblogic OutOfMemory exception的误解 -- thread limitation
不是全部的OutofMemory exception都是内存问题... 前几天有个客户的site报了下面错误: [ERROR][thread ] Could not start thread Time ...
- 页面加载后累加,自加1&&判断数字是否为两位数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Android学习(十五) 系统服务
一.常用系统服务 后台Service在系统启动时被SystemService开启 1.MountService:监听是否有SD卡安装和移除. 2.ClipboardService:提供剪切板功能. 3 ...