[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 ...
随机推荐
- 安装mathtype出问题卸载后 office2016打开mathtype弹错误窗口
解决方法:找到 C:\Program Files (x86)\Microsoft Office\root\Office16\STARTUP目录下的MathType Commands 6 For Wor ...
- APP功能性测试-4
弱网络测试 使用fiddler模拟低速环境 使用fiddler抓取手机上某个应用的包 手机连接fiddler fiddler 代理地址127.0.0.1默认端口8888 只抓http协议(https, ...
- jmeter接口测试--获取token
Jmeter进行接口测试-提取token 项目一般都需要进行登陆才能进行后续的操作,登陆有时发送的请求会带有token,因此, 需要使用后置处理器中的正则表达式提取token,然后用BeanShell ...
- 给移动硬盘安装rhel7
本机是win8.1的系统,但不想给电脑装双系统,所以想给移动硬盘里安装rhel7移动硬盘是750G的在网上搜了很多方法,我采取了两个方法:方法一.1.取一个U盘,用软碟通把rhel7的iso文件写进了 ...
- C#非托管跨线程委托调试
使用C#调用mingw的so文件,拿视频数据回wpf的界面进行显示,注册了回调函数.C++在调用回调函数时遇到了委托被回收的问题,提示:“类型的已垃圾回收委托进行了回调.这可能会导致应用程序崩溃.损坏 ...
- [leetcode-636-Exclusive Time of Functions]
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- Python之tornado框架实现翻页功能
1.结果如图所示,这里将html页面与网站的请求处理放在不同地方了 start.py代码 import tornado.ioloop import tornado.web from controlle ...
- 大数运算——hdu1042N!
一.题目回顾 题目链接:N! Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! ...
- [转]如何清空Chrome缓存和Cookie
当您使用浏览器(例如 Chrome)时,浏览器会将网站中的一些信息保存在其缓存和 Cookie 中. 清除这些内容可以解决某些问题,例如网站上的加载或格式设置问题. 在 Chrome 中 在计算机上打 ...
- UVA215 Spreadsheet
这道题题目大意就是计算带有单元格引用的各单元格的值. 这道题本身不难,有以下几个关键点: 1.如何判断一个单元格循环引用 2.注意对字符串的细致处理 我出现的错误出现在以上两个方面,思路本身是不难的. ...