[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 ...
随机推荐
- LeetCode 410——分割数组的最大值
1. 题目 2. 解答 此题目为 今日头条 2018 AI Camp 5 月 26 日在线笔试编程题第二道--最小分割分数. class Solution { public: // 若分割数组的最大值 ...
- 平面最近点对(HDU 1007)
题解:点击 #include <stdio.h> #include <string.h> #include <algorithm> #include <ios ...
- Unity UGUI 图片 轴对称效果 减少资源
制作UI的过程中,为了节省资源,对称的图一般美术切一半给我们 手动拼图 有时会出现拼接处出现裂缝或重叠 调整大小时也不方便 得一块一块调整 所以就用BaseMeshEffect 的ModifyMesh ...
- 关于react-redux中Provider、connect的解析
Provider 是什么 react-redux 提供的一个 React 组件 作用 把 store 提供给其子组件 //使用 redux 的 createStore 方法创建的一个 store co ...
- Uuuuuunity
foreach http://blog.csdn.net/byondocean/article/details/6871881 yield http://www.cnblogs.com/CareyS ...
- java获取本机器的IP(linux和windows)
目录 描述 方案描述 获取Windows下的IP 获取linux下的IP 判断操作系统的类型 最后将上面三个方法进行整合 参考 描述 由于项目是部署在集群上的,需要项目能够自动采集各机器的信息.jav ...
- spring环境搭建(简单实例)
1使用Maven导入需要的依赖(在project标签下) <properties> <spring_version>3.2.2.RELEASE</spring_versi ...
- 健康领域今年开始井喷了,养老地产和私人医生这两个领域目测成为下一轮BAT在健康领域布局的竞争方向
医疗行业做了六年多的时间,今年到了井喷的阶段,腾讯先是入股了丁香园,然后又一亿美金融资挂号网,春雨医生获得5000万美元的C轮融资,这是要上市的节奏.. 从互联网战略上,健康网和医疗网都是做资料刚开始 ...
- WCF身份验证二:基于消息安全模式的自定义身份验证
使用X509证书进行身份验证应该说是WCF安全模型中最”正常”的做法, 因为WCF强制要求使用证书加密身份数据, 离开了证书, 所有的身份验证机制拒绝工作, WCF支持的身份验证机制也相当复杂, 这里 ...
- Struts1防止表单重复提交
package org.zln.struts.action; import org.apache.struts.action.Action; import org.apache.struts.acti ...