LeetCode OJ:Subsets II(子集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.
例子:
If nums = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
求子集而已,不过这里的麻烦是子集的数字可能是重复的,这样可能会出现相同的结果的情况,解决的方法是先排序,然后dfs,dfs的时候如果发现当前的数与前一个数相等的话那么跳过进行下一个。这样就不会出现相同的vector的情况,具体代码如下:
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
ret.clear();
tmp.clear();
rawVec = nums;
sort(rawVec.begin(), rawVec.end());
dfs();
return ret;
}
void dfs(int start)
{
ret.push_back(tmp); //首先push一个空集
for(int i = start; i < rawVec.size(); ++i){
if(i != start && rawVec[i] == rawVec[i - ]) continue; //这里的i != start;应该注意
tmp.push_back(rawVec[i]);
dfs(i + );
tmp.pop_back();
}
}
private:
vector<vector<int>> ret;
vector<int> tmp;
vector<int> rawVec;
};
下面是java代码,改成了没有全局变量的形式,比上面的看起来要简单一些,代码如下所示,方法和上面的也有些许的不同:
public class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> ret = new ArrayList<List<Integer>>();
List<Integer> tmp = new ArrayList<Integer>();
dfs(ret, tmp, nums, 0, nums.length);
return ret;
}
public void dfs(List<List<Integer>> ret, List<Integer> tmp, int [] nums, int start, int limit){
if(start > limit)
return;
else if(start == limit){
ret.add(new ArrayList<Integer>(tmp));
return;
}else{
ret.add(new ArrayList<Integer>(tmp));
for(int i = start; i < limit; ++i){
tmp.add(nums[i]);
dfs(ret, tmp, nums, i+1, limit);
tmp.remove((Integer)nums[i]);
while(i+1 < limit && nums[i+1] == nums[i]) //跳过所有的重复的数字
i++; //这里加完了之后while循环又会再加一次,正好跳过所有的重复的值
}
}
}
}
LeetCode OJ:Subsets II(子集II)的更多相关文章
- 【LeetCode OJ】Path Sum II
Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- 【LeetCode OJ】Palindrome Partitioning II
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...
- 【LEETCODE OJ】Single Number II
Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- LeetCode OJ——Pascal's Triangle II
http://oj.leetcode.com/problems/pascals-triangle-ii/ 杨辉三角2,比杨辉三角要求的空间上限制Could you optimize your algo ...
- 090 Subsets II 子集 II
给定一个可能包含重复整数的列表,返回所有可能的子集(幂集).注意事项:解决方案集不能包含重复的子集.例如,如果 nums = [1,2,2],答案为:[ [2], [1], [1,2,2], ...
- leetcode刷题-90子集 II
题目 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2]输出:[ [2], [1], [1,2,2], [ ...
- [LeetCode OJ] Linked List Cycle II—Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- LeetCode OJ 45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- Python的模块与函数以及与自动化的结合
3 模块与函数 3.1程序结构 python的程序由package,module,function组成,分别是包,模块,函数.模块是函数和类的集合,包,模块,函数之间的关系如下: 3.2模块 pyth ...
- django事物回滚
往数据库写入数据时,不经意间就会写入不完整的数据,我们称之为脏数据.事务管理(transaction)可以防止这种情况发生.事务管理一旦检测到写入异常,会执行回滚操作,即要么写入完整的数据,要么不写入 ...
- 系统性能模块psutil
psutil是一个跨平台库,能够轻松实现获取系统运行的进程和系统利用率(包括cpu.内存.磁盘.网络等)信息.它主要用于系统监控,分析和限制系统资源及进程的管理.它实现了同等命令行工具提供的功能,如p ...
- 备注字段长度控制JS
//变更原因备注字符长度控制 function checkChangeLength() { var field = $("#changeReasonDesc").val(); ma ...
- iOS 9 的新功能 universal links
什么是 universal links: (通用链接) 一种能够方便的通过传统 HTTP 链接来启动 APP, 使用相同的网址打开web page和 APP的方式. 第一点,链接打开网址 顾名思义 第 ...
- 使用CoreData存储数据
- (void)viewDidLoad { [super viewDidLoad]; //获取模型文件的路径 NSString *path=[[NSBundle mainBundle]pathForR ...
- Android 使用OpenCV的三种方式(Android Studio)
http://blog.csdn.net/sbsujjbcy/article/details/49520791 其实最早接触OpenCV是很久很久之前的事了,大概在2013年的5,6月份,当时还是个菜 ...
- complexHeatmap包画分类热图
用途:一般我们画热图是以连续变量作为填充因子,complexHeatmap的oncopoint函数可以以类别变量作为填充因子作热图. 用法:oncoPrint(mat, get_type = func ...
- MySQL数据库基本操作(三)
MySQL补充: mysql是关系型数据库,关系数据库,是建立在关系模型基础上的数据库,现实世界中的各种实体,以及实体之间的各种联系,均用关系模型(table)来表示.#关系模型就是指二维表格模型,因 ...
- C++中随机数的生成
1.随机数由生成器和分布器结合产生 生成器generator:能够产生离散的等可能分布数值 分布器distributions: 能够把generator产生的均匀分布值映射到其他常见分布,如均匀分布u ...