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)的更多相关文章

  1. 【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 ...

  2. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

  3. 【LeetCode OJ】Palindrome Partitioning II

    Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...

  4. 【LEETCODE OJ】Single Number II

    Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...

  5. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  6. LeetCode OJ——Pascal's Triangle II

    http://oj.leetcode.com/problems/pascals-triangle-ii/ 杨辉三角2,比杨辉三角要求的空间上限制Could you optimize your algo ...

  7. 090 Subsets II 子集 II

    给定一个可能包含重复整数的列表,返回所有可能的子集(幂集).注意事项:解决方案集不能包含重复的子集.例如,如果 nums = [1,2,2],答案为:[  [2],  [1],  [1,2,2],  ...

  8. leetcode刷题-90子集 II

    题目 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2]输出:[ [2], [1], [1,2,2], [ ...

  9. [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 ...

  10. LeetCode OJ 45. Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

随机推荐

  1. 学习pyhton需要做哪些准备工作

    1:知道python个版本是有差异的 2:既然有差异,那么如何在不同项目不同版本的开发环境; ----------------------------------------------------- ...

  2. html用a标签怎么提交表单

    如下代码请 帮忙完善function judgeDelete()   {     if(confirm("确定要删除吗?"))     {      window.location ...

  3. LeetCode:数据库技术【180-185】

    LeetCode:数据库技术[180-185] 180.连续出现的数字 题目描述 编写一个 SQL 查询,查找所有至少连续出现三次的数字. +----+-----+ | Id | Num | +--- ...

  4. Linux Shell基础 Shell基本知识

    概述 在 Linux 的脚本中,只要是基于 Bash语法写的Shell脚本第一行必须是"#!/bin/bash",用来声明此文件是一个脚本. 运行方式 Shell 脚本的运行主要有 ...

  5. STM32F4XX高效驱动篇2 I2C

    说到I2C很多用过STMF10X硬件I2C方式的工程师,都感觉有点头痛.大部分还是使用软件模拟的方式,I2C由于一般的工作频率是400,100KHz.所以在平凡读取,或所读数据量大时,使用这模拟的方式 ...

  6. 测试连接oracle数据库耗时

    maven项目 主程序:ConnOracle.java package org.guangsoft.oracle; import java.sql.Connection; import java.sq ...

  7. java图形验证码

    用java实现验证码的生成,以下代码是一个controller,可以直接使用 package org.jxnd.tongxuelu.controller; import java.awt.Color; ...

  8. R/Bioconductor包的下载和安装,升级

    R包:基本包(自动加载)和推荐包(安装R时也会下载,但需要手动加载),拓展包(其他包,手动加载). 安装好的包将被放在一个指定的目录下.这个目录被称为库(Library).当需要使用到某一个包的时候, ...

  9. linux虚拟机ping通主机

    右键虚拟机,选择网络适配器,设置为桥接模式.然后关闭主机防火墙,ping就行了(一直ping是没有参数的)

  10. Vue.js学习笔记 第二篇 样式绑定

    Class绑定的对象语法 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...