【一天一道LeetCode】#90. Subsets II
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note: 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],
[]
]
(二)解题
题目大意:求给定数集中所有子集,注意数集中有重复的数字。
可以参考:【一天一道LeetCode】#78. Subsets,但是按照78题中的解法再最后去重的话就显得有些麻烦,于是采用回溯法在过程中去重。
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<int> temp;
vector<vector<int>> ret;
int last;
sort(nums.begin(),nums.end());//首先需要排序,便于在过程中去重
dfsSubset(ret,nums,temp,0,last);
return ret;
}
void dfsSubset(vector<vector<int>>&ret , vector<int>& nums , vector<int>& temp , int i ,int& last)
{
ret.push_back(temp);
for(int j = i ; j < nums.size() ; j++)
{
if(last == nums[j]) continue;//记录上一次push进去的数,如果相同,就代表重复了,需要去除
temp.push_back(nums[j]);
dfsSubset(ret,nums,temp,j+1,last);
last = nums[j];//每次记录push进去的数
temp.pop_back();//回溯到上一次的结果
}
}
};
【一天一道LeetCode】#90. Subsets II的更多相关文章
- [LeetCode] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- LeetCode 90. Subsets II (子集合之二)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- leetCode 90.Subsets II(子集II) 解题思路和方法
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- [LeetCode] 90. Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- Leetcode#90 Subsets II
原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- LeetCode Problem 90. Subsets II
python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...
随机推荐
- vue环境搭建过程中,遇到的坑爹的问题
1,在配置package.json下载node依赖包时,执行$cnpm install过程中,这个过程是比较漫长的,尤其的这种core i5配置的电脑,简直有点卡的人怀疑人生,后来动了下有消息输出,我 ...
- Kafka(转载)
Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cloudera.Apache Storm.Spa ...
- 微信内置浏览器 如何小窗不全屏播放视频?也可以尝试canvas.
设置属性: <video height="100%" width="100%" autoplay="autoplay" control ...
- Android项目实战(四十五):Usb转串口通讯(CH34xUARTDriver)
需求为:手机usb接口插入一个硬件,从硬件上获取数据 例如:手机usb插入硬件A,A通过蓝牙通讯获取设备a.b的数据,作为中转站(可以做些数据处理)将数据(设备a.b产生的)传给手机程序. 设备A也可 ...
- Linux下DIR,dirent,stat等结构体详解
摘自:http://www.liweifan.com/2012/05/13/linux-system-function-files-operation/ 最近在看Linux下文件操作相关章节,遇到了这 ...
- DOS/Windows 文本格式与 Unix 文本各式转换
命令简介: dos2unix是将Windows格式文件转换为Unix.Linux格式的实用命令.Windows格式文件的换行符为\r\n ,而Unix&Linux文件的换行符为\n. dos2 ...
- let 和 const 命令
let 命令 基本用法 ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. { let a = 10; var b = 1; } a / ...
- Node.js 子进程
稳定性: 3 - 稳定 Node 通过 child_process 模块提供了 popen(3) 数据流. 它能在非阻塞的方式中,通过 stdin, stdout, 和 stderr 传递数据. (请 ...
- Hadoop加速器GridGain
GridGain的Hadoop加速器 像GridGain等内存网格产品(IMDG)不仅可以作为简单的缓存,加速Hadoop中MapReduce计算也是IMDG的一个亮点.这样内存计算领域又多了一种思路 ...
- iOS 中捕获截屏操作
转自:iOS知识小集 在iOS 7后,苹果提供了UIApplicationUserDidTakeScreenshotNotification通知来告诉App用户做了截屏操作.苹果的描述如下: // T ...