Subsets II 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/subsets-ii/description/


Description

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

Solution

class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int>& nums) {
vector<vector<int> > result;
if (nums.empty())
return result;
result.resize(1);
sort(nums.begin(), nums.end());
int lastNum = nums[0], size = 1;
int i, j;
for (i = 0; i < nums.size(); i++) {
if (lastNum != nums[i]) {
lastNum = nums[i];
size = result.size();
}
int newResSize = result.size();
for (j = newResSize - size; j < newResSize; j++) {
result.push_back(result[j]);
result.back().push_back(nums[i]);
}
}
return result;
}
};

解题描述

这道题是求幂集的升级版:原始集合含有重复元素,生成幂集的过程中要去重。这道题在第一版的算法基础上,对重复数字开始的地方进行区分(记录不应重复添加新数字的集合数目),以避免对前面产生的数组再全部进行新数字的添加而导致重复的集合的出现。

[Leetcode Week8]Subsets II的更多相关文章

  1. 【leetcode】Subsets II

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

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

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

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

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

  4. Java for LeetCode 090 Subsets II

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

  5. [LeetCode] 90. Subsets II 子集合 II

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

  6. 深度优先搜索算法(DFS)以及leetCode的subsets II

    深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...

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

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

  8. [Leetcode Week8]Subsets

    Subsets 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets/description/ Description Given a set ...

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

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

随机推荐

  1. Windows下nginx作为静态资源服务器使用

    一.Nginx下载与安装 1.nginx官方下载地址:http://nginx.org/ 2.下载完后将压缩包解压即可 3.nginx配置文件为根目录下conf\nginx.conf 二.Nginx常 ...

  2. Python-类-函数参数-takes 0 positional arguments but 1 was given

    在学习Python基础的时候,在创建某一个shownametest()函数,解析器会报错 TypeError: shownametest() takes 0 positional arguments ...

  3. [OpenCV]DMatch类和KeyPoints类:特征点匹配

    DMatch struct CV_EXPORTS_W_SIMPLE DMatch { CV_WRAP DMatch() : queryIdx(-), trainIdx(-), imgIdx(-), d ...

  4. sqlserver2012 查询远程数据库

    EXEC sp_addlinkedserver 'LinkName','','SQLOLEDB','121.43.177.236'EXEC sp_addlinkedsrvlogin 'LinkName ...

  5. Beat 冲刺 (3/7)

    队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 1.界面的修改与完善 展示GitHub当日代码/文档签入记 ...

  6. const在c/c++中的区别

    #include <iostream> using namespace std; int main() { ; ; }; ; i < sizeof array / sizeof *a ...

  7. java基础知识-冒泡排序

    //冒泡排序,从数组前面向后循环比较 public static void sort1(int[] aa){ int size=aa.length; int temp; //循环数组 for(int ...

  8. 【bzoj3196】Tyvj 1730 二逼平衡树 线段树套Treap

    题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在区间内的前驱(前驱定义 ...

  9. P1268 树的重量

    题目描述 树可以用来表示物种之间的进化关系.一棵“进化树”是一个带边权的树,其叶节点表示一个物种,两个叶节点之间的距离表示两个物种的差异.现在,一个重要的问题是,根据物种之间的距离,重构相应的“进化树 ...

  10. 【题解】HAOI2011Problem b

    第一次接触莫比乌斯反演,总之脑子都快要炸掉了……好难啊!本蒟蒻表示根本无法理解呜呜呜呜呜……不过在机房DL的帮助下总算是磕磕绊绊的A掉了这一题: 这道题目要我们的求的是:(1)ΣiΣj [gcd(i, ...