LeetCode(90) 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],
          []
]
分析
求带有重复元素的序列的全子集;
用动态规划的思想,逐个向前i-1的元素的子集中添加第i个元素,添加时需要判重,若已存在,则不添加。
AC代码
class Solution {
public:
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        vector<vector<int> > ret(1, vector<int>());
        if (nums.empty())
            return ret;
        sort(nums.begin(), nums.end());
        int size = nums.size();
        for (int i = 0; i < size; ++i)
        {
            ret = subSets(ret, nums, i);
        }
        return ret;
    }
    vector<vector<int> > subSets(vector<vector<int> > &ret, vector<int> &nums, int &idx)
    {
        vector<int> tmp;
        int count = ret.size();
        //对于每一个已有子集合,加入新元素
        for (int i = 0; i < count; ++i)
        {
            //当前集合
            tmp = ret[i];
            tmp.push_back(nums[idx]);
            if (find(ret.begin(), ret.end(), tmp) != ret.end())
                continue;
            ret.push_back(tmp);
        }//for
        return ret;
    }
};
LeetCode(90) Subsets II的更多相关文章
- LeetCode(275)H-Index II
		
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
 - LeetCode(90):子集 II
		
Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...
 - LeetCode(52) N-Queens II
		
题目 Follow up for N-Queens problem. Now, instead outputting board configurations, return the total nu ...
 - LeetCode(78) Subsets
		
题目 Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...
 - Leetcode(213)-打家劫舍II
		
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在 ...
 - LeetCode(47)Permutations II
		
题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...
 - LeetCode(122)  Best Time to Buy and Sell Stock II
		
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
 - LeetCode(154) Find Minimum in Rotated Sorted Array II
		
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
 - LeetCode(113) Path Sum II
		
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
 
随机推荐
- Codeforces 1143B(思维、技巧)
			
自己水平太低,不丢人. 结论是最后选取的数后缀一定是若干个9,暴举即可.然而暴举也有暴举的艺术. ll n; ll dfs(ll n) { if (n == 0) return 1; if (n &l ...
 - 1-26HashSet简介
			
Set的特点 Set里面存储的元素不能重复,没有索引,存取顺序不一致. package com.monkey1024.set; import java.util.HashSet; /** * Set的 ...
 - CentOS7下使用Docker容器化.net Core 2.2
			
一.使用 yum 安装(CentOS 7下) Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker . 通过 una ...
 - python入门之实例-商品选择
			
需求: 显示一系列商品,根据序号选择商品 li = ["手机","电脑","电视"] #函数enumerate在for循环遍历的时候,会默认 ...
 - C51 笔记
			
一 关于宏常量的长度:C51中定义一个常数宏(默认是16位的),如果用宏表示一个32位的宏而不加'L'标志的话就会出错.如 #define BLOCK_A_BASEADDR 18*64*1024 / ...
 - HTML 5的革新——语义化标签(二)
			
HTML 5的革新之一:语义化标签二文本元素标签.分组元素标签. HTML 5的革新——语义化标签(一)中介绍了一些HTML5新加的一些节元素,一张页面中结构元素构成网页大体,但是也需要其他内容来填充 ...
 - git命令收集
			
$ git clone ... $ git status 查看状态 $ git commit -am "XXX" 提交信息 $ git commit -am "XXXX& ...
 - IT人怎样防止过劳死?如何成为时间的主人?
			
投行的朋友还没走几天,搜狐的一位同胞又去了.又是过劳死! 每当读到这类新闻,IT人无不反镜自照,顾影自怜.无法拼爹拼钱的我们,似乎只有拼命了.生活好惨淡啊! 有人说:年轻人,悠着点儿!立刻 ...
 - Android View 背景选择器编写技巧
			
在项目中选择器的使用是非常多的,以下是本人在项目中的一些常用的背景选择器的写法 带边框下划线背景选择器效果图: 上面布局中放了10个CheckBox,然后设置了CheckBox的背景图片位,背景选择器 ...
 - 不同版本的 Tomcat 设置用户名密码 的方法
			
Tomcat : tomcat根目录\conf\tomcat-users.xml,找到 <tomcat-users> 标签,在后面添加 <user username="ad ...