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的更多相关文章

  1. [Leetcode Week8]Subsets II

    Subsets II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets-ii/description/ Description Given ...

  2. [LeetCode] 90.Subsets II tag: backtracking

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  3. [leetcode]90. Subsets II数组子集(有重)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  4. Java for LeetCode 090 Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  5. [LeetCode] 90. Subsets II 子集合 II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  6. 【leetcode】Subsets (Medium) ☆

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  7. 深度优先搜索算法(DFS)以及leetCode的subsets II

    深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...

  8. LeetCode 90. Subsets II (子集合之二)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  9. LeetCode 78. Subsets(子集合)

    Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...

随机推荐

  1. cocos2d-x 中菜单类

    菜单相关类包含:菜单类和菜单项类,菜单类图,从类图可见Menu类继承于Layer. 菜单项类图,从图中可见所有的菜单项都是从BaseMenuItem继承而来的,BaseMenuItem是抽象类,具体使 ...

  2. CSP201503-1:图像旋转

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  3. json格式化显示样式js代码分享

    最近开发中需要在页面展示json.特整理了下代码,送给大家,希望能帮到有同样需求的朋友们. 代码: <html> <script src="http://cdn.bootc ...

  4. Python 3 学习笔记之——变量作用域、模块和包

    1. 变量作用域 Python 中,程序的变量并不是在哪个位置都可以访问的,访问权限决定于这个变量是在哪里赋值的.变量的作用域决定了在哪一部分程序可以访问哪个特定的变量名称.Python 的作用域一共 ...

  5. Dijkstra模板 dj斯特拉

    图论里非常常用的dijkstra,自己加了个路径查找,做个模板吧: ; struct Edge { int from,to,dist; Edge(int u, int v, int d):from(u ...

  6. HDU 4433 locker(DP)(2012 Asia Tianjin Regional Contest)

    Problem Description A password locker with N digits, each digit can be rotated to 0-9 circularly.You ...

  7. BufferedInputStream/BufferedOutputStream

    BufferedInputStream: public synchronized int read() throws IOException int res=bis.read(); System.ou ...

  8. asp.net页面中的Console.WriteLine结果如何查看

    其实用Console.WriteLine("xxxxx"),在asp.net Web程序,在输出窗口是不会输出结果的,应该用Debug.WriteLine("xxxxx& ...

  9. penLDAP学习笔记

    LDAP协议 目录是一组具有类似属性.以一定逻辑和层次组合的信息.常见的例子是通讯簿,由以字母顺序排列的名字.地址和电话号码组成.目录服务是一种在分布式环境中发现目标的方法.目录具有两个主要组成部分: ...

  10. lintcode-114-不同的路径

    114-不同的路径 有一个机器人的位于一个 m × n 个网格左上角. 机器人每一时刻只能向下或者向右移动一步.机器人试图达到网格的右下角. 问有多少条不同的路径? 注意事项 n和m均不超过100 样 ...