博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode] Subsets II
阅读量:6945 次
发布时间:2019-06-27

本文共 1378 字,大约阅读时间需要 4 分钟。

Given a collection of integers that might contain duplicates, 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,2], a solution is:

[  [2],  [1],  [1,2,2],  [2,2],  [1,2],  []]

思路:

和一样,只是这里原容器中有重复的元素,于是就增加一个去重。去重的方法我用的还是与leetcode上的另一个一样,开辟一个数组记录当前元素是否使用过。如果当前元素等于前一个元素,且前一个元素没有使用,那么当前元素就不能使用。

class Solution {public:    vector
> res; void dfs(vector
&s, bool used[], int index, vector
tmp) { if(index==s.size()) { res.push_back(tmp); return; } dfs(s, used, index+1, tmp); if(!used[index]) { if(index!=0 && s[index]==s[index-1] && !used[index-1]) return; used[index] = true; tmp.push_back(s[index]); dfs(s, used, index+1, tmp); used[index] = false; } } vector
> subsetsWithDup(vector
&s) { sort(s.begin(), s.end()); vector
tmp; int n = s.size(); bool *used = new bool [n]; memset(used, false, n); dfs(s, used, 0, tmp); return res; }};
View Code

 

 

 

转载于:https://www.cnblogs.com/jiasaidongqi/p/4206784.html

你可能感兴趣的文章
oracle 拼接字符串的两种方式
查看>>
Base PyQt4, Simple Web APP Framwork
查看>>
如何设计定向爬虫
查看>>
初识toolstack——XEN的XenServer管理的核心
查看>>
20篇教你得到酷炫效果的JQuery教程
查看>>
安装程序找不到Office.zh-cn\OfficeMUI问题
查看>>
如何使用VIM的列编辑模式 [转]
查看>>
System.Collections简介
查看>>
POJ-3211 Washing Clothes[01背包问题]
查看>>
Android--MediaPlayer(实现列表选歌,上一首,下一首,清空播放列表,搜索本地音乐文件)...
查看>>
ODE的buggy例程分析
查看>>
判断文章/帖子操作权限
查看>>
计算机英文缩写
查看>>
Windows2003 SQL2005解决系统Administrator密码不知道的问题
查看>>
curl常用的5个例子(转)
查看>>
wCF 问题收集页
查看>>
《ASP.NET MVC4 WEB编程》学习笔记------.net mvc实现原理ActionResult/View
查看>>
1、传感器概述
查看>>
需求分析报告和需求规格说明书有什么区别
查看>>
转:Vmware Exsi使用简要说明
查看>>