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 ...
随机推荐
- hdu2586 LCA带边权的Targan算法
bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=2586 #include<bits/stdc++.h> using names ...
- RTOS之CMSIS-RTOS
CMSIS-RTOS 是实时操作系统的通用 API.它提供了标准化的编程接口,它只是封装了RTX/embos,以后还可能封装freeRTOS,uc/os等等第三方OS,CMSIS RTOS是ARM现在 ...
- (转)Unity中使用C#实现Zip包的压缩与解压
使用SharpZipLib库,下载地址为:http://icsharpcode.github.io/SharpZipLib/ /************************************ ...
- 通用全局CSS样式
PC全局样式 *{padding:0;margin:0;} div,dl,dt,dd,form,h1,h2,h3,h4,h5,h6,img,ol,ul,li,table,th,td,p,span,a{ ...
- Linux scp 命令使用方法
scp 命令: 1.将本地文件拷贝到远程:scp 文件名 用户名@计算机IP或者计算机名称:远程路径 2.从远程将文件拷回本地:scp 用户名@计算机IP或者计算机名称:文件名 本地路径 3.将本 ...
- 第七章 设计程序架构 之 设计HTTP模块和处理程序
1. 概述 HTTP模块和处理程序,可以让程序员直接跟HTTP请求交互. 本章内容包括 实现同步和异步模块及处理程序以及在IIS中如何选择模块和处理程序. 2. 主要内容 2.1 实现同步和异步模块及 ...
- debian使用apt安装时出现“更换介质,插入驱动器"/media/chrom/"再按回车键”的提示,无法从网络安装,解决?
原文链接:https://www.zhihu.com/question/22132663 nano /etc/apt/sources.list把那出现的那行注释掉:含CD盘的一行:然后apt-get ...
- react中的context的基础用法
context提供了一种数据共享的机制,里面有两个关键概念——provider,consumer,下面做一些key features描述. 参考网址:https://react.docschina.o ...
- Docker镜像的目录存储讲解
我们成功安装完docker后,执行命令行sudo docker run hello-world, 如果是第一次执行,则会从远程拉取hello-world的镜像到本地,然后运行,显示hello worl ...
- 记录我开发工作中遇到HTTP跨域和OPTION请求的一个坑
我通过这篇文章把今天工作中遇到的HTTP跨域和OPTION请求的一个坑记录下来. 场景是我需要在部署在域名a的Web应用里用JavaScript去消费一个部署在域名b的服务器上的服务.域名b上的服务也 ...