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 ...
随机推荐
- Hexo+yilia博客添加背景音乐
个人主页:https://www.yuehan.online 现在博客:www.wangyurui.top 第一步: 打开网易云音乐的官网:https://music.163.com/ 第二步: 搜索 ...
- mysql学习笔记—常用sql语句
sql注意事项: SQL 对大小写不敏感:SELECT 与 select 是相同的 某些数据库系统要求在每条 SQL 语句的末端使用分号. CREATE DATABASE CREATE DATABAS ...
- make menuconfig 时出现 mixed implicit and normal rules: deprecated syntax
這是 make 的版本問題!不清楚為何要這樣限制? 將此行 config %config: scripts_basic outputmakefile FORCE改成 ...
- 20170405-STO库存转储单
1.工厂间转储: (1)MB1B 移动类型 301 工厂到工厂(一步)转账,->简单明了一步转储过账后会产生 GR,MITA增加了,MIZH减少了,MB03, **会产生 GR,如果俩工厂 标准 ...
- numpy利用数组进行数据处理
将条件逻辑表述为数组运算 numpy.where()是一个三目运算的表达式 In [34]: xarr = np.array([1.1,1.2,1.3,1.4,1.5]) In [35]: yarr ...
- BigDecimal类型、Long类型判断值是否相等,以及BigDecimal加减乘除
//Long是需要比较精度的,所以要用longValueif(project.getFriendId().longValue() != friendId.longValue()) { return t ...
- Loadrunder脚本篇——Running Time setting之Additional attributes
作用说明 为Vuser脚本提供额外自定义参数.额外参数设置可应用于所有Vuser脚本类型. 说明: Add:添加一个参数 Remove:移除一个参数 可以在这里先输入一些常用的参数名及对应的值,类似常 ...
- HTML学习笔记(下)
表格标签 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3 ...
- Shell编程之Expect自动化交互程序
一.Expect自动化交互程序 1.spawn命令 通过spawn执行一个命令或程序,之后所有的Expect操作都会在这个执行过的命令或程序进程中进行,包括自动交互功能. 语法: spawn [ 选项 ...
- Windows下MarialDB使用
命令行控制启动和关闭:mysqld --console #这样启动ctrl+c即为关闭 启动:双击mysqld.exe即可 #此为后台启动 关闭:mysqladmin -uroot -pr ...