【Leetcode】【Medium】Subsets II
Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2]
, a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
解题:
先对S进行排序,然后继续按照Subsets I的思路,
由于出现了重复元素,需要分别考虑:
当出现的数字和上一个数字不同,保持原返回队列不变,对新出现的数字,分别插入已有数组的后面,形成新数组,加入返回值队列中;此时记录形成的新数组个数X;
当出现的数字和上一个数字相同,保持原返回队列不变,从原返回队列的后方遍历X个已有数组,对这X个数组插入这个相同的数字,形成新数组,加入返回队列中;X值不变;
代码:
class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
sort(S.begin(), S.end());
vector<vector<int> > ret;
ret.push_back(vector<int> ());
int duplicate_record = ; for (int i = ; i < S.size(); ++i) {
int pre_size = ret.size(); if (i== || S[i] != S[i-]) {
for (int j = ; j < pre_size; ++j) {
vector<int> new_item(ret[j]);
new_item.push_back(S[i]);
ret.push_back(new_item);
}
duplicate_record = ret.size() / ; } else if (S[i] == S[i-]) {
for (int j = pre_size - ; j >= pre_size - duplicate_record; --j) {
vector<int> new_item(ret[j]);
new_item.push_back(S[i]);
ret.push_back(new_item);
}
} } return ret;
}
};
【Leetcode】【Medium】Subsets II的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- LeetCode解题报告—— Word Search & Subsets II & Decode Ways
1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...
- 【leetcode刷题笔记】Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【leetcode刷题笔记】Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【leetcode刷题笔记】Subsets
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- 【leetcode刷题笔记】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 al ...
- 【leetcode刷题笔记】Populating Next Right Pointers in Each Node II
What if the given tree could be any binary tree? Would your previous solution still work? Note: You ...
随机推荐
- 用PL/sql连接oracle 弹窗出现 could not resolve the connect identifier specified 这个错误
1 错误如下图: 图1 2.可能原因: 配置oracle客户端中tnsnames.ora文件时,把数据库名弄错,如下图: 图2 箭头所指位置出错.箭头处应该为我们安装时的数据库名(通常是orcl).而 ...
- python+requests抓取页面图片
前言: 学完requests库后,想到可以利用python+requests爬取页面图片,想到实战一下.依照现在所学只能爬取图片在html页面的而不能爬取由JavaScript生成的图片,所以我选取饿 ...
- Robot Framework自动化测试二(元素定位)
前言 在学习的过程中,可能会误认为Robot framework 只是个web UI测试工具,更正确的理解Robot framework是个测试框架,之所以可以拿来做web UI层的自动化是国为我们加 ...
- JDBC(3)-使用PreparedStatement接口实现增、删、改操作
1.PreparedStatement接口引入 PreparedStatement是Statement的子接口,属于预处理操作,与直接使用Statement不同的是,PreparedStatement ...
- Flyway数据库版本控制
前言:最近工作上遇到个问题,项目开发过程中,开发代码可以通过svn来版本控制,但数据库又该如何来管理呢?多个人接触数据库,当对表.字段或数据修改后,又怎么来同步呢?经过苦寻,发现了个叫flyway的开 ...
- shell -- 获取绝对路径
readlink -f <file> readlink -m <file> 会把file的相对路径转化为绝对路径 几个选项的区别: -f, --canonicalize can ...
- python风味之list创建
单重for循环 >>> [x * x for x in xrange(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 单重for循环+if条件 & ...
- Apache 反向代理 丢失Authorization
我后端API的服务器是Tomcat,而后端API验证是通过存放在头部Authorization的token值进行验证的. 我在测试Apache作为前端html解析的服务器时, 利用反向代理,把Api请 ...
- linux 查看服务器系统资源和负载,以及性能监控
1.查看磁盘 df -h 2.查看内存大小 free [-m|g]#按MB,GB显示内存 3.查看每个进程的情况 cat /proc/5346/status PID 4.查看负载 uptime 5.查 ...
- 查找CPU使用率过高的线程
1.在编写程序中有时候设置不恰当,休眠时间不够,一般情况下4核的电脑CPU使用率一直大于23%,8核的大于13%就有可能是这种情况 解决方法: 在VS查看并行线程利用CPU使用工具ProcessExp ...