【Leetcode】【Medium】Subsets
Given a set of distinct integers, S, 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 S = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
解题思路:
题目要求返回值要从大到小排列,因此先对序列S排序o(nlogn);
假设S为abcd...,然后遍历S中每个数,
对a,返回集合[ [ ], [a] ]
对b,返回集合{ [ ], [a], [b], [a, b] }
由此看出,每出现一个新值,就是保存返回集合中原有数组不变,并在每个原有数组后加上新值,组合成新数组,添加到返回集合的后面;
代码:
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
sort(S.begin(), S.end());
vector<vector<int> > ret;
ret.push_back(vector<int> ());
for (int i = ; i < S.size(); ++i) {
int pre_size = ret.size();
for (int j = ; j < pre_size; ++j) {
vector<int> new_item(ret[j]);
new_item.push_back(S[i]);
ret.push_back(new_item);
}
}
return ret;
}
};
附录:
C++ vector 浅复制、深复制
【Leetcode】【Medium】Subsets的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【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算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
随机推荐
- numpy多维矩阵,取出第一行或者第一列,方法和df一样
# 定义一个多维矩阵 arr = np.array([[1,2,3], [4,5,6], [7,8,9]]) # 取出第一行 arr[0,:] # 取出第一列 arr[:,0]
- 读取P12格式证书的密钥
不想存储p12证书内容,只想存储证书密钥,可通过以下实现读取证书的密钥出来: package com.zat.ucop.service.util; import org.apache.commons. ...
- ViewData与ViewBag的区别
本文导读:在asp.net mvc程序设计中,传递数据常常会用到viewdata.viewbag.ViewData是一个字典集合,通过key值读取对应的value:ViewBag是动态类型,作用和Vi ...
- (转)MySQL5.6主从复制技术
原文:http://www.cnblogs.com/ilifeilong/category/1014799.html MySQL5.6复制技术(4)-MySQL主从复制过滤参数 摘要: 复制的过滤主要 ...
- intellij idea NoClassDefFoundError javax.swing.UIManager
今天启动idea报 NoClassDefFoundError javax.swing.UIManager 可是明明配置好了java 环境 ,后来仔细想了一下只配置了java的bin目录在PATH里 随 ...
- WPF实现无刷新动态切换多语言(国际化)
1. 在WPF中国际化使用的是 .xaml文件的格式 如图:Resource Dictionary (WPF) 2. 创建默认的语言文件和其他语言文件 这里以英语为默认语言,新建一个 Resource ...
- [转]error MSB4018: The "GenerateResource" task failed unexpectedly
本文转自:https://github.com/Microsoft/msbuild/issues/364 After uninstall Visual Studio 2015 Update 1 RC ...
- 常用软件下载开发环境七牛镜像Java、Node、Mongo
[jdk1.8] Linux:http://soft.yzeng.cc/jdk18/jdk-8u202-linux-x64.tar.gz Windows:http://soft.yzeng.cc/jd ...
- 在 Azure 上创建和链接 MySQL 数据库
本快速入门介绍了如何使用 Azure 门户创建并连接 MySQL 数据库.在本教程中完成的所有操作均符合 1 元试用条件. 开始之前如果您还没有 Azure 账户,可以申请 1 元试用账户 步骤1:创 ...
- 版本控制器之SVN
1.开发中的实际问题 1.1 小明负责的模块就要完成了,就在即将Release之前的一瞬间,电脑突然蓝屏,硬盘光荣牺牲!几个月来的努力付之东流——需求之一:备份! 1.2 这个项目中需要一个很复杂的功 ...