【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 ...
随机推荐
- link 标签中“rel=stylesheet”的作用
最近在复习我的培训项目学子商城项目的时候在引入外部css的时候忘记加上了rel=stylesheet(因为以前看别人给的模板有所以就加了上去,所以并没有太大印象) 那rel=stylesheet到底起 ...
- ionic3 引入第三方库(jquery)
安装 npm install jquery npm install @types/jquery 在需要的ts文件中引入,一定要在最顶端 import * as $ from '../../../nod ...
- spring.net AOP
AOP 术语 通知(Advice): 通知描述了切面要完成的任务,同时还描述了何时执行这个任务. 连接点(Joinpoint): 程序中应用通知的地方称为连接点,这个点可以是方法被调用时,异常抛出时 ...
- MySQL where 表达式
where 条件表达式 对记录进行过滤,如果没有指定where子句,则显示所有记录. 在where表达式中,可以使用MySQL支持的函数或运算符.
- Python获取当前路径下的配置文件
Python获取当前路径下的配置文件 有的时候想读取当前目录下的一个配置文件.其采用的办法是: import os # 获取当前路径 curr_dir = os.path.dirname(os.pat ...
- Bash编程(4) 参数与变量
1. 变量命名 变量命名只能使用数字.下划线.字母,且仅能以下划线或字母开头. 变量很少使用单个字母,单个字母一般用于循环或读取一次性文件的时候. 例: while IFS=: read login ...
- codeforces 675 C ——Money Transfers——————【思维题】
Money Transfers time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- C#继承中构造函数,成员变量的执行顺序
public class Animal { static string baseClassName; protected string _skincolor; Instancevariable iv ...
- CentOS7 一键安装KMS服务【整理】
KMS,是 Key Management System 的缩写,也就是密钥管理系统.这里所说的 KMS,毋庸置疑就是用来激活 VOL 版本的 Windows 和 Office 的 KMS 啦.经常能在 ...
- 静态代码块,构造代码块,main()
静态代码块 随Class 加载而加载,为Class 作初始化: 在main() 之前加载: 只执行一次: 构造代码块 随对象的创建而加载,为对象作初始化 public class day04 { pu ...