[Leetcode Week8]Subsets
Subsets 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/subsets/description/
Description
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: 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],
[]
]
Solution
class Solution {
public:
vector< vector<int> > subsets(vector<int>& nums) {
int size = nums.size(), j;
unsigned long long bits = 1 << size;
unsigned long long i;
vector< vector<int> > result;
for (i = 0; i < bits; i++) {
vector<int> temp;
for (j = 0; j < size; j++) {
if ((i >> j) & 1)
temp.push_back(nums[j]);
}
result.push_back(temp);
}
return result;
}
};
解题描述
这道题其实就是简单的求幂集。我使用的是穷举每一位的有无两种情况来构造所有的子集。
[Leetcode Week8]Subsets的更多相关文章
- [Leetcode Week8]Subsets II
Subsets II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets-ii/description/ Description Given ...
- [LeetCode] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- Java for LeetCode 090 Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- 【leetcode】Subsets (Medium) ☆
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- 深度优先搜索算法(DFS)以及leetCode的subsets II
深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...
- LeetCode 90. Subsets II (子集合之二)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- LeetCode 78. Subsets(子集合)
Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...
随机推荐
- Centos6.5
1.首先我们需要检测系统是否自带安装mysql # yum list installed | grep mysql 2.如果发现有系统自带mysql,果断这么干 # yum -y remove mys ...
- python 网络编程(远程执行命令与粘包)
远程执行命令 先来学习一个新模块 , 一会用到的.. 新模块: subprocess 执行系统命令 r = subprocess.Popen('ls',shell=True,stdout=subpro ...
- python 基础篇 15 内置函数和匿名函数
------------------------>>>>>>>>>>>>>>>内置函数<<< ...
- CSP201512-1: 数位之和
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...
- DFS——CodeForces740DAlyona and a tree
一.题目回顾 题目链接:Alyona and a tree Examples Input 52 5 1 4 61 71 13 53 6 Output 1 0 1 0 0 Input 59 7 8 ...
- 第十一次ScrumMeeting会议
第十一次ScrumMeeting 时间:2017/11/18 4:00-4:30 地点:主203 人员:全体人员 照片: 工作情况 名字 今日计划 明天的工作 遇到的困难 蔡帜 讨论策划详情\确定WB ...
- DPDK如何抓包
原创翻译,转载请注明出处. DPDK的librte_pdump库,提供了在DPDK框架下抓包的功能.这个库通过完全复制Rx和Tx的mbuf到一个新的内存池,因此它降低应用程序的性能,所以只推荐在调试的 ...
- vue里的this
vue中methods对象里的函数, this指向的都是当前实例或者组件.
- 【EasyNetQ】- 订阅
EasyNetQ订阅者订阅消息类型(消息类的.NET类型).一旦通过调用Subscribe方法设置了对类型的订阅,就会在RabbitMQ代理上创建一个持久队列,并且该类型的任何消息都将被放置在队列中. ...
- 【bzoj3362/3363/3364/3365】[Usaco2004 Feb]树上问题杂烩 并查集/树的直径/LCA/树的点分治
题目描述 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤40000)条的不同的垂直或水平的道路连结着农场,道路的长度不超过1000.这些农场的分布就像下面的地图一样, 图中农场用F ...