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. 使用SQL-Front启动MySQL8.0报错

    这学期学习数据库,电脑上分别装有phpStudy(自带的MySQL版本为5.5)和MySQL8.0.11,于是想用phpStudy中的SQL Front连接到8.0的数据库.手动开启8.0的MySQL ...

  2. Codechef April Challenge 2019 游记

    Codechef April Challenge 2019 游记 Subtree Removal 题目大意: 一棵\(n(n\le10^5)\)个结点的有根树,每个结点有一个权值\(w_i(|w_i\ ...

  3. history.back(-1) 和history.go(-1) 有什么区别?

    history.back(-1) 返回上一页,当前页面的数据都没有保存下来.就像当前也没有出现过一样. history.go(-1)    返回上一页,当前页的内容都保存下来了,包括session,等 ...

  4. sqlserver安装检测不通过 (重新启动失败)

    打开注册表,HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager 删除“PendingFileRenameOperat ...

  5. 如何查看ubuntu系统版本信息

    第一种方法: hadoop@master:~$ cat /proc/version Linux version 4.4.0-21-generic (buildd@lgw01-21):Linux内核版本 ...

  6. 用 PHP文件引入css样式

    html: <link rel="stylesheet" type="text/css" href="http://www.mysite.com ...

  7. 关于Django字段类型中 blank和null的区别

    blank 设置为True时,字段可以为空.设置为False时,字段是必须填写的.字符型字段CharField和TextField是用空字符串来存储空值的. 如果为True,字段允许为空,默认不允许. ...

  8. VS启动调试速度异常的缓慢问题

    方法1: 1. 进入vs2017 2.工具 --选项 -- IntelliTrace 关闭此功能 方法2: 1.由于缓存数据太多,需要重置下vs的开发环境 2.打开visual studio 的命名窗 ...

  9. libguestfs手册(3): virt命令

    guestmount root# guestmount -a ubuntutest1.img -m /dev/sda1 ubuntutestp1 root# cd ubuntutestp1/root: ...

  10. Lesson 29 Taxi!

    Text Captain Ben Fawcett has bought an unusual taxi and has begun a new serivice. The 'taxi' is a sm ...