题目

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;
}
};

GitHub测试程序源码

LeetCode(90) Subsets II的更多相关文章

  1. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  2. LeetCode(90):子集 II

    Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...

  3. LeetCode(52) N-Queens II

    题目 Follow up for N-Queens problem. Now, instead outputting board configurations, return the total nu ...

  4. LeetCode(78) Subsets

    题目 Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...

  5. Leetcode(213)-打家劫舍II

    你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在 ...

  6. LeetCode(47)Permutations II

    题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...

  7. 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 ...

  8. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  9. 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 ...

随机推荐

  1. IMG 的alt和title的区别(转自 百度空间--路云的世界)

    图片标签img中alt与title的区别 图片标签img中alt与title的区别 可能很多新手在做站内优化的时候,不明白图片标签img中alt与title的区别,今天为大家说一下其中的区别. 大家可 ...

  2. git与GitHub(二)

    昨天在安装完git之后,出了一个问题,虽然暂时有解决的办法,但是由于电脑中了病毒并且配置低下等原因,这个问题的解决办法目前还有待考证.遇到的问题是这样的: 前提是想试一下git在命令行里的命令:于是: ...

  3. 5、两个栈实现队列------------>剑指offer系列

    题目 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路 栈1: 用于入队列存储 栈2: 出队列时将栈1的数据依次出栈,并入栈到栈2中 栈2出栈即栈1的底部数据 ...

  4. spark常用参数

    val conf = new SparkConf().setAppName("WordCount_groupBy").setMaster("local") // ...

  5. Gitlab User Guide

    Installation Configuration Set user name and email Add SSH keys Repository Create New Repository Clo ...

  6. iOS Block的本质(二)

    iOS Block的本质(二) 1. 介绍引入block本质 通过上一篇文章Block的本质(一)已经基本对block的底层结构有了基本的认识,block的底层就是__main_block_impl_ ...

  7. jenkins+phantomjs环境搭建及使用

    #jenkins+phantomjs 前端性能自动化测试的安装和使用#gcc GNU编译器套件 https://gcc.gnu.org/ #nginx 高性能的HTTP和反向代理服务器 http:// ...

  8. HDU 5418 Victor and World (可重复走的TSP问题,状压dp)

    题意: 每个点都可以走多次的TSP问题:有n个点(n<=16),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短. 思路: 给了很多重边,选最小的留下即可.任意点可能无法直接到达, ...

  9. hihoCoder hiho一下 第四十六周 博弈游戏·Nim游戏·三( sg函数 )

    题意: 给出几堆石子数量,每次可以取走一堆中任意数量的石头,也可以将一堆分成两堆,而不取.最后取走者胜. 思路: 先规矩地计算出sg值,再对每个数量查SG值就可以了.最后求异或和.和不为0的就是必赢. ...

  10. UIButton Making the hit area larger

    http://stackoverflow.com/questions/808503/uibutton-making-the-hit-area-larger-than-the-default-hit-a ...