import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
* Source : https://oj.leetcode.com/problems/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],
* []
* ]
*
*/
public class SubSet2 { private List<List<Integer>> result = null; /**
*
*
* @return
*/
public List<List<Integer>> subset (int[] arr) {
result = new ArrayList<List<Integer>>();
List<Integer> set = new ArrayList<Integer>();
Arrays.sort(arr);
recursion(arr, 0, set);
return result;
} private void recursion (int[] arr, int index, List<Integer> set) {
if (index == arr.length) {
return;
}
// 初始化上一次出栈的元素为当前将要进栈的元素-1,为了不和将要进栈的元素相同
int last = arr[index]-1;
for (int i = index; i < arr.length; i++) {
// 如果上一次出栈的元素等于准备进栈的元素,则说明两个元素一样,跳过
if (last == arr[i]) {
continue;
}
set.add(arr[i]);
List<Integer> temp = new ArrayList<Integer>(set);
result.add(temp);
recursion(arr, i + 1, set);
last = set.remove(set.size()-1);
}
} private static void print (List<List<Integer>> list) {
for (List<Integer> arr : list) {
System.out.println(Arrays.toString(arr.toArray(new Integer[arr.size()])));
}
System.out.println();
} public static void main(String[] args) {
SubSet2 subSet2 = new SubSet2();
int[] arr = new int[]{1,2,2};
int[] arr1 = new int[]{1,2,3,3,3,3,4};
print(subSet2.subset(arr));
print(subSet2.subset(arr1)); }
}

leetcode — subsets-ii的更多相关文章

  1. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  2. [LeetCode] Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  3. [leetcode]Subsets II @ Python

    原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might cont ...

  4. [Leetcode] Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  5. [LeetCode] Subsets II [32]

    题目 Given a collection of integers that might contain duplicates, S, return all possible subsets. Not ...

  6. [Leetcode] subsets ii 求数组所有的子集

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  7. [LeetCode]Subsets II生成组合序列

    class Solution {//生成全部[不反复]的组合.生成组合仅仅要採用递归,由序列从前往后遍历就可以. 至于去重,依据分析相应的递归树可知.同一个父节点出来的两个分支不能一样(即不能与前一个 ...

  8. LeetCode:Subsets I II

    求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...

  9. Subsets II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Subsets II - LeetCode 注意点 有重复的数字 数组可能是无序的,要先排序 解法 解法一:递归,只需要在Subsets中递归写法的基础上 ...

  10. 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 ...

随机推荐

  1. Android 第四次作业

    一.团队成员: 段嗣跃:https://www.cnblogs.com/duansiyue/ 陈素伟:https://www.cnblogs.com/aX-qhu/ 二.APK链接: https:// ...

  2. 将Redhat,CentOS,Ubuntu虚拟机的IP设为静态IP的方法

    一般在主机上创建的虚拟机默认是通过DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)网络协议来动态生成的,这样会导致你安装的虚拟机的IP地址是动态变化 ...

  3. 在IDEA中spring程序可以正常运行,但IDE报错Could not autowire.

    这个问题只会在使用xml文件配置bean时遇到,使用自动配置和java配置没有这种错误. 遇到这个问题首先要确定配置文件已经正确的放在了resources文件夹内,resources文件夹就是clas ...

  4. Docker-Compose入门

    转:https://blog.csdn.net/chinrui/article/details/79155688

  5. RequireJS简单实用说明

    OM前端框架说明 om前端框架采用RequireJS,RequireJS  是一个JavaScript模块加载器.它非常适合在浏览器中使用, 它非常适合在浏览器中使用,但它也可以用在其他脚本环境, 就 ...

  6. PageHelper分页插件的使用

    大家好!今天写ssm项目实现分页的时候用到pageHelper分页插件,在使用过程中出现了一些错误,因此写篇随笔记录下整个过程 1.背景:在项目的开发的过程中,为了实现所有的功能. 2.目标:实现分页 ...

  7. CRT破解版

    1.先去https://www.ttrar.com/html/VanDyke-SecureCRT.html上面下载一个CRT软件 2.下载一个注册机 http://www.ddooo.com/soft ...

  8. 【常用指令】git+服务器+数据库

    git日程操作 服务器常用操作 ①同步master git pull origin master ②创建分支 git checkout -b 分支名 ③常规操作 git diff git add 文件 ...

  9. [Swift]LeetCode909. 蛇梯棋 | Snakes and Ladders

    On an N x N board, the numbers from 1 to N*N are written boustrophedonically starting from the botto ...

  10. 微信小程序中样式问题

    1.去除button按钮的默认样式 这是button按钮自带的默认样式 button { position:relative; display:block; margin-left:auto; mar ...