LeetCode(78) Subsets
题目
Given a set of distinct integers, nums, 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 nums = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
分析
求给定一个集合的子集!
该题目与LeetCode 77 Combinations类似,都是动态规划思想的应用!
同理,不能采用暴力法解决问题,因为会带来很高的复杂度。
仔细分析,我们发现包含n个元素的集合的子集有以下三个部分组成:
- 第一部分,该集合前n-1个元素组成的集合的子集;
- 第二部分,该集合中最后一个元素构成的一个子集合;
- 第三部分,对该集合前n-1个元素组成的集合的子集,每一个都添加最后一个元素;
注意,对于返回的集合的全部子集包括一个空子集!
AC代码
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
if (nums.empty())
return vector<vector<int>>();
sort(nums.begin(), nums.end());
vector<vector<int>> ret = getSubsets(nums);
//不要丢掉最后的空子集
ret.push_back(vector<int>());
return ret;
}
vector<vector<int>> getSubsets(vector<int>& nums) {
if (nums.empty())
return vector<vector<int>>();
int len = nums.size();
//初始化一个包含空元素的结果vector
vector<vector<int> > ret;
//用0~len-1个原数组元素,建立一个新数组
vector<int> tem_nums(nums.begin(), nums.begin() + len - 1);
vector<vector<int>> tmp = getSubsets(tem_nums);
int t_size = tmp.size();
//第一部分,0~len-1所有元素的真子集
for (int i = 0; i < t_size; i++)
{
ret.push_back(tmp[i]);
}//for
//第二部分,加入第len个元素
ret.push_back(vector<int>(1, nums[len - 1]));
//第三部分,0~len-1所有元素的真子集,加入第len个元素
for (int i = 0; i < t_size; i++)
{
tmp[i].push_back(nums[len - 1]);
ret.push_back(tmp[i]);
}//for
return ret;
}
};
LeetCode(78) Subsets的更多相关文章
- LeetCode(78):子集
Medium! 题目描述: 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3 ...
- LeetCode(90) Subsets II
题目 Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
随机推荐
- hdu1811 Rank of Tetris 并查集+拓扑排序
#include <stdio.h> #include <string.h> #include <vector> #include <queue> us ...
- python之请求报文对比(假定最多二维字典)
两段请求报文,判断不一样的key和value,只判断d2里和d1不同的值,和全部不同的key ok_req={ "version": "9.0.0", &quo ...
- Qt容器类之一:Qt的容器类介绍
一.介绍 Qt库提供了一套通用的基于模板的容器类,可以用这些类存储指定类型的项.比如,你需要一个大小可变的QString的数组,则使用QVector<QString>. 这些容器类比STL ...
- UVA - 11082 Matrix Decompressing
2. B - Matrix Decompressing 题意:定义一个R*C的正整数矩阵(1<=R,C<=20),设Ai为前i行所有元素之和,Bi为前i列所有元素之和. 题目已知R,C和数 ...
- Educational Codeforces Round 18 D
Description T is a complete binary tree consisting of n vertices. It means that exactly one vertex i ...
- 503 Next Greater Element II 下一个更大元素 II
给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它 ...
- python_8(模块)
第1章 模块 1.1 概述 1.2 模块的分类 1.2.1 内置模块 1.2.2 扩展模块 1.2.3 模块安装 1.2.4 自定义模块第2章 模块之内置模块 2.1 collections模块 2. ...
- HtmlUnit爬取Ajax动态生成的页面内容
HtmlUnit说白了就是一个浏览器,这个浏览器是用Java写的无界面的浏览器,正因为其没有界面,因此执行的速度还是可以滴. HtmlUnit提供了一系列的API,这些API可以干的功能比较多,如表单 ...
- Python behave in BDD
BDD概念 全称 Behavior-driven development 中文 行为驱动开发 概念 是敏捷软件开发技术的一种,鼓励各方人员在一个软件项目里交流合作,包括开发人员.测试人员和非技术人员或 ...
- MyBatis学习(四)
前言 最近比较松懈,下班回家后也懒得学习了.今晚实在是看不下去了,争取时间学习.社会上有这么多的资源,就看谁能抢的多吧.今天就说说MyBatis的动态SQL吧 正文 动态 SQL 通常要做的事情是有条 ...