【Lintcode】018.Subsets II

题目:

Given a list of numbers that may has duplicate numbers, return all possible subsets

Notice

  • Each element in a subset must be in non-descending order.
  • The ordering between two subsets is free.
  • The solution set must not contain duplicate subsets.

Example

If S = [1,2,2], a solution is:

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

题解:

Solution 1 ()

class Solution {
public:vector<vector<int> > subsetsWithDup(vector<int> S) {vector<vector<int> > res;vector<int> v;sort(S.begin(), S.end());Dfs(S, res, v, 0);return res;}void Dfs(vector<int> S, vector<vector<int> > &res, vector<int> &v, int pos) {res.push_back(v);for (int i = pos; i < S.size(); ++i) {if (i == pos || S[i] != S[i - 1]) {v.push_back(S[i]);Dfs(S, res, v, i + 1);v.pop_back();}}}
};

  To solve this problem, it is helpful to first think how many subsets are there. If there is no duplicate element, the answer is simply 2^n, where n is the number of elements. This is because you have two choices for each element, either putting it into the subset or not. So all subsets for this no-duplicate set can be easily constructed:

num of subset

  • (1 to 2^0) empty set is the first subset
  • (2^0+1 to 2^1) add the first element into subset from (1)
  • (2^1+1 to 2^2) add the second element into subset (1 to 2^1)
  • (2^2+1 to 2^3) add the third element into subset (1 to 2^2)
  • ....
  • (2^(n-1)+1 to 2^n) add the nth element into subset(1 to 2^(n-1))

Then how many subsets are there if there are duplicate elements? We can treat duplicate element as a spacial element. For example, if we have duplicate elements (5, 5), instead of treating them as two elements that are duplicate, we can treat it as one special element 5, but this element has more than two choices: you can either NOT put it into the subset, or put ONE 5 into the subset, or put TWO 5s into the subset. Therefore, we are given an array (a1, a2, a3, ..., an) with each of them appearing (k1, k2, k3, ..., kn) times, the number of subset is (k1+1)(k2+1)...(kn+1). We can easily see how to write down all the subsets similar to the approach above.

Solution 2 ()

class Solution {
public:vector<vector<int> > subsetsWithDup(vector<int> &S) {vector<vector<int> > res{{}};sort(S.begin(), S.end());for (int i = 0; i < S.size(); ) {int cnt = 0;while (cnt + i < S.size() && S[cnt + i] == S[i]) {++cnt;}int size = res.size();for (int j = 0; j < size; ++j) {vector<int> instance = res[j];for (int k = 0; k < cnt; ++k) {instance.push_back(S[i]);res.push_back(instance);}}i += cnt;}return res;}
};

Solution 3 ()

class Solution {
public:vector<vector<int> > subsetsWithDup(vector<int> &S) {vector<vector<int> > res{{}};sort(S.begin(), S.end());int size = 1;int last = !S.empty() ? S[0] : 0;for (int i = 0; i < S.size(); ++i) {if (last != S[i]) {last = S[i];size = res.size();}int newsize = res.size();for (int j = newsize - size; j < newsize; ++j) {res.push_back(res[j]);res.back().push_back(S[i]);}}return res;}
};

 

转载于:https://www.cnblogs.com/Atanisi/p/6866474.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392781.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

多线程1

1-1 进程 程序是静止的&#xff0c;运行中的程序就是进程。进程的三个特征&#xff1a; 动态性 &#xff1a; 进程是运行中的程序&#xff0c;要动态的占用内存&#xff0c;CPU和网络等资源。独立性 &#xff1a; 进程与进程之间是相关独立的&#xff0c;彼此有自己的独立内存区…

go 列出已经安装的包_Go 安装教程

一、在 Windows 上安装 Go 环境首先在 Go 官网 下载 Windows 系统下的一键安装包。然后双击打开该文件&#xff0c;一直点 Next 就行。注意这里默认是安装到 C 盘&#xff0c;建议不要修改&#xff0c;因为环境变量会自动设置&#xff0c;如果安装到其他盘&#xff0c;那么可能…

【转】spin_lock、spin_lock_irq、spin_lock_irqsave区别

为什么80%的码农都做不了架构师&#xff1f;>>> 转自&#xff1a;http://blog.csdn.net/luckywang1103/article/details/42083613 void spin_lock(spinlock_t *lock);void spin_lock_irq(spinlock_t *lock);void spin_lock_irqsave(spinlock_t *lock, unsigned lon…

七年级计算机上教学计划,初一教学计划模板锦集5篇

初一教学计划模板锦集5篇时光在流逝&#xff0c;从不停歇&#xff0c;我们又将迎来新的教学工作&#xff0c;我们要好好计划今后的教育教学方法。那么一份同事都拍手称赞的教学计划是什么样的呢&#xff1f;以下是小编为大家整理的初一教学计划5篇&#xff0c;仅供参考&#xf…

程序员实际情况_程序员实际上是做什么的?

程序员实际情况What do programmers actually do? What can they be working on?程序员实际上是做什么的&#xff1f; 他们可以做什么&#xff1f; In this video from an Airbnb software engineer, you will learn about what programmers do on a day-to-day basis. She …

leetcode 1365. 有多少小于当前数字的数字(排序)

给你一个数组 nums&#xff0c;对于其中每个元素 nums[i]&#xff0c;请你统计数组中比它小的所有数字的数目。 换而言之&#xff0c;对于每个 nums[i] 你必须计算出有效的 j 的数量&#xff0c;其中 j 满足 j ! i 且 nums[j] < nums[i] 。 以数组形式返回答案。 示例 1&…

spring整合springmvc案例

面试遇到过上机操作&#xff0c;不知道小伙伴们遇到过没。 案例。 1、新建web项目&#xff0c;找到相关的jar包。 转载于:https://www.cnblogs.com/sjzxs/p/11158116.html

我的世界服务器玩家在线时间,将公布上线时间?我的世界中国版网易520前瞻

【17173专稿&#xff0c;转载请注明出处】《我的世界》中国版最近一段时间动作不断。网易CEO丁磊在财报电话会议上公布了《我的世界》手游版会在7月份推出&#xff0c;结合《我的世界》中国版的公告提及&#xff1a;”《我的世界》中国版即将在暑期上线“。如此看来手游版和PC版…

ftpwebrequest 无法加载或初始化请求的服务提供程序_jvm之类加载机制

什么是类加载每个编写的".java"拓展名类文件都存储着需要执行的程序逻辑&#xff0c;这些".java"文件经过Java编译器编译成拓展名为".class"的文件&#xff0c;".class"文件中保存着Java代码经转换后的虚拟机指令&#xff0c;当需要使…

【284天】我爱刷题系列(43)

叨叨两句 身体是灵魂的载体&#xff0c;灵魂是身体的指引&#xff0c;用心维护、驯化你的身体&#xff0c;构建通道&#xff0c;指引它将力量与情绪宣泄在你想做出成绩的领域&#xff0c;神奇的事情就会发生&#xff0c;哈哈。牛客网——java专项练习023 1 SuppressWarnings(“…

基于python渗透测试_Python中基于属性的测试简介

基于python渗透测试by Shashi Kumar Raja由Shashi Kumar Raja Python中基于属性的测试简介 (Intro to property-based testing in Python) In this article we will learn a unique and effective approach to testing called property-based testing. We will use Python , p…

leetcode144. 二叉树的前序遍历(迭代)

给定一个二叉树&#xff0c;返回它的 前序 遍历。示例:输入: [1,null,2,3] 1\2/3 输出: [1,2,3]代码 /*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val…

矩阵的理解经典博客

矩阵理解一&#xff1a;https://blog.csdn.net/myan/article/details/647511 矩阵理解二&#xff1a;https://blog.csdn.net/myan/article/details/649018 矩阵理解三&#xff1a;https://blog.csdn.net/myan/article/details/1865397 关键结论&#xff1a; 1. 首先有空间&…

推断图片格式

Linux/Unix下系统推断文件类型并不根据文件名称&#xff0c;也即不会根据文件后缀来推断文件的类型。从网上下载了一个图片&#xff0c;没有后缀&#xff0c;希望可以正确推断出格式。以便于共享到其它平台&#xff0c;该怎么办呢&#xff1f; 不同文件类型的文件头部信息不同&…

云服务器怎么设置域名,云服务器域名设置在哪里

可能不同的云服务厂商域名设置的方式略有不同&#xff0c;不过&#xff0c;大体来讲&#xff0c;方法应该都差不多的。下面我们以1.打开浏览器&#xff0c;搜索西部数码官网并登陆账号密码&#xff0c;到会员中心。2.进入管理中心后&#xff0c;在左侧的业务管理中找到3.点击服…

RHCE 学习笔记(9) 网络管理

n这一节本来按照教学大纲应该是学习SSH&#xff0c;不过SSH有很多网络相关的知识&#xff0c;因此老师把网络内容提前了一些。网络的基本知识例如IP&#xff0c;DNS&#xff0c;DHCP&#xff0c;路由协议等常识就不在此解释了。 RHEL查看网卡的相关信息很容易&#xff0c;ifcon…

leetcode 1207. 独一无二的出现次数(map+set)

给你一个整数数组 arr&#xff0c;请你帮忙统计数组中每个数的出现次数。 如果每个数的出现次数都是独一无二的&#xff0c;就返回 true&#xff1b;否则返回 false。 示例 1&#xff1a; 输入&#xff1a;arr [1,2,2,1,1,3] 输出&#xff1a;true 解释&#xff1a;在该数组…

地图上绘制任意角度的椭圆_地图上的总椭圆

地图上绘制任意角度的椭圆或者&#xff0c;如何选择下班后去海滩的最佳方式 (Or, how to choose the best way to walk to the beach after work) It was a cool autumn evening when Hila Kloper and I were thinking of going to the beach after work. The beach is about 2…

【NOI2014】起床困难综合症 贪心

从高到低按位贪心&#xff0c;讨论一下初始0或1&#xff0c;分别暴力算出结果是什么 如果一开始0就能得1当然直接ans垒起来 如果1能得1而且当前m够用&#xff0c;那也垒起来&#xff0c;同时m减掉 否则gg 2min的代码 1 #include <bits/stdc.h>2 #define miaom(x,y) ((x &…

用原生js封装get方法

get方法的封装 首先我们看一下用原生js来发送请求的步骤: 1.创建请求对象 .var xhrnew XMLHttpRequest(); 2.创建open方法确认请求方式和地址 xhr.open(get,url) ps(记住get方法有参数的话在url后面用?符号连接再加上参数如:url?num3,多个参数用&符号连接); 3.监听事件…