Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).

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],
[]
]

78. Subsets 的拓展,这题数组里面可能含有重复元素。

Python:

class Solution(object):
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums.sort()
result = [[]]
previous_size = 0
for i in xrange(len(nums)):
size = len(result)
for j in xrange(size):
# Only union non-duplicate element or new union set.
if i == 0 or nums[i] != nums[i - 1] or j >= previous_size:
result.append(list(result[j]))
result[-1].append(nums[i])
previous_size = size
return result

Python:

class Solution2(object):
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
i, count = 0, 1 << len(nums)
nums.sort() while i < count:
cur = []
for j in xrange(len(nums)):
if i & 1 << j:
cur.append(nums[j])
if cur not in result:
result.append(cur)
i += 1 return result

Python:  

class Solution3(object):
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
self.subsetsWithDupRecu(result, [], sorted(nums))
return result def subsetsWithDupRecu(self, result, cur, nums):
if not nums:
if cur not in result:
result.append(cur)
else:
self.subsetsWithDupRecu(result, cur, nums[1:])
self.subsetsWithDupRecu(result, cur + [nums[0]], nums[1:])  

C++:

class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int> &nums) {
vector<vector<int>> result(1);
sort(nums.begin(), nums.end());
size_t previous_size = 0;
for (size_t i = 0; i < nums.size(); ++i) {
const size_t size = result.size();
for (size_t j = 0; j < size; ++j) {
// Only union non-duplicate element or new union set.
if (i == 0 || nums[i] != nums[i - 1] || j >= previous_size) {
result.emplace_back(result[j]);
result.back().emplace_back(nums[i]);
}
}
previous_size = size;
}
return result;
}
};

  

类似题目:

[LeetCode] 78. Subsets 子集合

All LeetCode Questions List 题目汇总

[LeetCode] 90. Subsets II 子集合 II的更多相关文章

  1. LeetCode 78. Subsets(子集合)

    Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...

  2. LeetCode 90. Subsets II (子集合之二)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  3. [LeetCode] 90.Subsets II tag: backtracking

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  4. [leetcode]90. Subsets II数组子集(有重)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  5. [LeetCode] 916. Word Subsets 单词子集合

    We are given two arrays A and B of words.  Each word is a string of lowercase letters. Now, say that ...

  6. [LeetCode] 90. Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  7. Leetcode#90 Subsets II

    原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...

  8. leetCode 90.Subsets II(子集II) 解题思路和方法

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  9. leetcode 90. subsets

    解题思路: 要生成子集,对于vector 中的每个数,对于每个子集有两种情况,加入或不加入. 因此代码: class Solution { public: void subsetG(vector< ...

随机推荐

  1. Pure C static coding analysis tools

    Cppcheck - A tool for static C/C++ code analysiscppcheck.sourceforge.netCppcheck is a static analysi ...

  2. 使用批处理打包C#开发程序

    最近项目接近尾声,测试比较频繁,每天需要发布多个版本 @echo off set zip=C:\Program Files\7-Zip\7z.exe set timestamp=%date:~6,4% ...

  3. Beta冲刺(2/7)——2019.5.23

    所属课程 软件工程1916|W(福州大学) 作业要求 Beta冲刺(2/7)--2019.5.23 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪万里 ...

  4. 关于vue的v-for遍历不显示问题

    实属不才,因为好久没看vue导致忘光了,然后发生了这么小的一个问题,惭愧. 注:vue的注册的el一定要放嘴最外层,不要和v-for放在一起,否则不会显示,因为可以这样讲,el包含的是一个容器,而v- ...

  5. python_并发编程——数据共享

    1.数据共享 实现进程之间的数据共享 from multiprocessing import Manager,Process class MyPro(Process): def __init__(se ...

  6. HDU - 3535:AreYouBusy (分组背包)

    题意:给你n个工作集合,给你T的时间去做它们.给你m和s,说明这个工作集合有m件事可以做,它们是s类的工作集合(s=0,1,2,s=0说明这m件事中最少得做一件,s=1说明这m件事中最多只能做一件,s ...

  7. C# .net core 相对路径转绝对路径 (官方示例)

    public static string GetAbsolutePath(string relativePath) { FileInfo _dataRoot = new FileInfo(typeof ...

  8. c++处理字符串string.find()与string::npos

    1. string s  = “xxx”; int a = s.find(‘x’); 如果没有匹配到,那么a = string::npos;

  9. mysql ,limit 的使用

    //offset参数指定要返回的第一行的偏移量.第一行的偏移量为0,第二行的偏移量为1.count指定要返回的最大行数.LIMIT offset,count; mysql> select * f ...

  10. python 判断一个字符串组合后,是否在另一个字符串中

    code #coding=utf- def getdic(s): dic = {} for i in s: if (i not in dic): dic[i] = else: dic[i] += re ...