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. [HEOI/TJOI2016]序列

    Description: 给你一个序列,每个数可能变化为另一个数,每次最多有一个数变化 求最长的子序列,无论如何变化,这个子序列都不下降 Hint: \(n \le 10^5\) Solution: ...

  2. myeclipse集成maven

    myeclipse集成maven 打开myeclipse---Window----Preferences---Maven4MyEclipse--Installations----Add,如图所示: 这 ...

  3. 【CSS 第六天】三种简历

    1.盒子模型 2.三种简历 利用float和CSS制作内容一致,但是样式不一致的三种简历. 代码 3.效果 style-1 3.2效果2 效果三

  4. java学习之路--零碎的知识笔记

    java运算符: 自增自减运算符: int b = ++a; 拆分运算过程为: a=a+1=4; b=a=4, 最后结果为b=4,a=4 前缀自增自减法(++a,--a): 先进行自增或者自减运算,再 ...

  5. node.js搭建Web服务器

    Node.js 博客搭建 一. 学习需求 Node 的安装运行 会安装node,搭建node环境 会运行node. 基础模块的使用 Buffer:二进制数据处理模块 Event:事件模块 fs:文件系 ...

  6. 大数相加 Big Num

    代码: #include<stdio.h>#include<algorithm>#include<iostream>#include<string.h> ...

  7. 记一次通过c#运用GraphQL调用Github api

    阅读目录 GraphQL是什么 .net下如何运用GraphQL 运用GraphQL调用Github api 结语 一.Graphql是什么 最近在折腾使用Github api做个微信小程序练练手,本 ...

  8. 实战深度学习OpenCV(一):canny边缘检测

    利用canny边缘检测,我们可以很好地得到哦一个图像的轮廓,下面是基于C++的,这是我们通过这段代码得到的结果: #include "pch.h" #include <ios ...

  9. [Swift]LeetCode63. 不同路径 II | Unique Paths II

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  10. [Bash]LeetCode195. 第十行 | Tenth Line

    Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...