【LeetCode】90.Subsets II
Subsets II
Given a collection of integers that might contain duplicates, 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,2]
, a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
] 这类有重复值的情况怎么处理
SubSets的算法
class Solution {
public:
std::vector<std::vector<int> > subsets(std::vector<int> &nums) {
std::sort(nums.begin(), nums.end());
std::vector<std::vector<int> > res;
std::vector<int> vec;
subsets(res, nums, vec, );
return res;
}
private:
void subsets(std::vector<std::vector<int> > &res, std::vector<int> &nums, std::vector<int> &vec, int begin) {
res.push_back(vec);
for (int i = begin; i != nums.size(); ++i) {
vec.push_back(nums[i]);
subsets(res, nums, vec, i + );
vec.pop_back();
}
}
};
subsets([,,,]) = []
// push(1)
[, subsets([,,])] // if push N times in subsets([2,3,4]), the pop times is also N, so vec is also [1] after backtrack.
// pop(), push(2)
[, subsets([,])]
// pop(), push(3)
[, subsets([])]
// pop(), push(4)
[, subsets([])]
// pop()
Subsets II的解法
class Solution {
public:
std::vector<std::vector<int> > subsetsWithDup(std::vector<int> &nums) {
std::sort(nums.begin(), nums.end());
std::vector<std::vector<int> > res;
std::vector<int> vec;
subsetsWithDup(res, nums, vec, );
return res;
}
private:
void subsetsWithDup(std::vector<std::vector<int> > &res, std::vector<int> &nums, std::vector<int> &vec, int begin) {
res.push_back(vec);
for (int i = begin; i != nums.size(); ++i)
if (i == begin || nums[i] != nums[i - ]) {
vec.push_back(nums[i]);
subsetsWithDup(res, nums, vec, i + );
vec.pop_back();
}
}
};
【LeetCode】90.Subsets II的更多相关文章
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【一天一道LeetCode】#90. Subsets II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode Problem 90. Subsets II
python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【LeetCode】78. Subsets (2 solutions)
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【LeetCode】基本计算器II
[问题]实现一个基本的计算器来计算一个简单的字符串表达式的值.字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格 .整数除法仅保留整数部分. 输入: "3+2*2" ...
- 【LeetCode 】N皇后II
[问题]n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法.给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: ...
随机推荐
- HDU 3086 马拉车模板
模板,但是对这个算法还是不太清楚,真实不明觉厉.... #include <iostream> #include <cstdio> #include <string.h& ...
- python打包成为exe文件
pyinstaller 库的使用 PyInstaller是一个十分有用的第三方库,它能够在Windows.Linux.Mac OS X 等操作系统下将 Python 源文件打包,通过对源文件打包,Py ...
- centos安装消息队列beanstalkd
起因:开始想在windows安装beanstalkd,可以找了很多资料都没有成功.最终还是妥协.在虚拟机上装一个centos系统,然后在centos上安装beanstalkd供windows使用 yu ...
- Thinkphp 架构笔记
多个模块的时候,公共模块Common必须和其他模块放在同一个目录下,否则拓展配置“LOAD_EXT_CONFIG”会无效
- 百度the big talk节目
主要事件 2015-03-09期:硅谷峰会:智能机器人&对话沃兹尼亚克 2015-03-02期:硅谷峰会:创新金融和智能城市 2015-02-15期:硅谷峰会:数字生物学和数字医药 2015- ...
- 读书笔记--Head First Python 目录
1.初识python 2.共享你的代码 3.文件与异常 4.持久共享 5.推导数据 6.定制数据对象 7.web开发 8.移动应用开发 9.管理你的数据 10.扩展你的web应用 11.处理复杂性 其 ...
- spring深入学习(六)-----springmvc
MVC设计模式 有过一定开发经验的人肯定都知道这个模式,先简单介绍下这种模式,然后再去讨论为啥要这么设计: 传统的web应用中应该主要包括这些组件,不同组件负责不同的模块. 数据实体:POJO 数据层 ...
- day38 06-MyEclipse配置Schema约束
- 将自己的代码托管到github - 秦时明月 - CSDN博客
步骤: 1.建立自己的github 2.安装github客户端,并配置身份 3.建立github项目 4.将github项目库下载到本地 5.提交本地代码到github 详细操作: 1.github网 ...
- 当spark遇见hbase
一.使用sbt引入hbase依赖包 "org.apache.hbase" % "hbase-server" % "2.1.0", " ...