leetcode5: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],
[]
]
方法一:迭代
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<int> v;
vector<vector<int>> res;
res.push_back(v);
for(int i=;i<nums.size();i++)
{
for(int j=res.size()-;j>=;j--)
{
v.assign(res[j].begin(),res[j].end());
v.push_back(nums[i]);
res.push_back(v);
}
}
return res;
}
};
方法二:递归
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<vector<int>> res;
res.reserve(pow(,nums.size()));
vector<int> v;
subset(res,nums,,v);
return res;
}
private:
void subset(vector<vector<int>>& res,vector<int>& s,int start,vector<int>& v)
{
if(start == s.size())
{
res.push_back(v);
return;
}
subset(res,s,start+,v);
v.push_back(s[start]);
subset(res,s,start+,v);
v.pop_back();
}
};
leetcode5:subsets问题的更多相关文章
- Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- Subsets
Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...
- [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:Subsets I II
求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...
- 【leetcode】Subsets II (middle) ☆
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【leetcode】Subsets (Medium) ☆
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- [Leetcode] Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 42. Subsets && Subsets II
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
随机推荐
- Solr相似度算法二:Okapi BM25
地址:https://en.wikipedia.org/wiki/Okapi_BM25 In information retrieval, Okapi BM25 (BM stands for Be ...
- solr特点六: DIH (从数据源导入数据)
在这个结构化数据和非结构化数据的数量都很庞大的年代,经常需要从数据库.XML/HTML 文件或其他数据源导入数据,并使数据可搜索.过去,要编写自定义代码才能创建到数据库.文件系统或 RSS 提要的自定 ...
- leetcode 字符串转整数(atoi)
实现atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即为整数的值. ...
- C#基础笔记(第十七天)
1.复习 ref 传地址 用的是同一块内存 一个改变另一个也随着改变 return n1 > n2 ? n1 : n2; 三元表达式 int max=GetMax(1,2,3,4,5,6,); ...
- JSON is undefined. Infopath Form People Picker in SharePoint 2013
After some analysis, we found that, this is a known defect with the Microsoft and it is being fixed ...
- 在 Cef 中实现 C++ 与 JavaScript 交互场景分析
此文已由作者邓佳佳授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验 本文主要介绍 CEF 场景中 C++ 和 JavaScript 交互(以下简称 JS Bridge)中的一些重 ...
- 【ocp-12c】最新CUUG OCP-071考试题库(48题)
48.(10-12)choose the best answer View the Exhibit and examine the description for the PRODUCTS and S ...
- IDEA 引入外部jar包 pom 配置,防止打包失败
1. <!--添加外部依赖--> <dependency> <groupId>Ice</groupId> ...
- PHP和javascript中url编码解码详解
在实际开发中,我们可能会遇到路径编码解码的问题,下面总结了一下: PHP中: 1.urlencode(编码),urldecode(解码) $a = urlencode('http://www.baid ...
- SQL Server——存储过程(Stored Procedure)、事物、触发器
存储过程(proc 或 procedure) 存储过程(Stored Procedure),计算机用语,是一组为了完成特定功能的SQL语句集,是利用SQL Server所提供的Transact-SQL ...