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. 2018-2019-2 网络对抗技术 20162329 Exp4 恶意代码分析

    目录 Exp4 恶意代码分析 一.基础问题 问题1: 问题2: 二.系统监控 1. 系统命令监控 2. 使用Windows系统工具集sysmon监控系统状态 三.恶意软件分析 1. virustota ...

  2. Windows 2003 防火墙开启后无法访问FTP解决办法

    最近遇到一个比较郁闷的问题,服务器开启防火墙后电脑就无法访问服务器的防火墙,但是防火墙又不能关,在例外中添加端口21也不管用. 后来在网上终于找到了一个解决方案,可以在不关掉防火墙的情况下访问服务器的 ...

  3. H5_ 表单及其他新增和改良元素

    1. form属性 formaction属性 formmethod属性 formenctype属性 formtarget属性 <!DOCTYPE html> <html lang=& ...

  4. Java for Android 第三周学习总结

    第五章 核心类 java.lang.Object中的方法: clone(创建并返回该对象的一个副本.实现这个方法的一个类,将支持对象的复制) equals(将该对象和传入的对象进行比较.必须实现这个算 ...

  5. vue 组件传值

    父组件传值给子组件 <list v-show="listLen" :listdata="list" :tipMsg="tipMsg" ...

  6. BUAA-OO-第二单元总结

    OO第二单元总结 一.第五次作业 1.1 设计策略与架构 第五次作业要求的是完成设计支持一架傻瓜电梯的电梯系统.考虑到需要数据结构存放所有的请求,因此构建了FloorRequests类用来存放所有的请 ...

  7. sqlmap Windows 安装教程

    第一步:下载 python :https://www.python.org/downloads/    (这里有python各种版本,但是一般建议安装3和2.7) sqlmap:https://git ...

  8. Ubuntu ARM更改为国内源

    关键词:ubuntu  arm  ubuntu-ports  国内源  镜像  阿里源  apt apt-get install update  0%working  速度慢  rk3399  开发板 ...

  9. 计算CPU的MIPS

    如图: MIPS=主频/CPI*(10^6), 其中CPI的计算公式为CPI=总的时钟周期数/总的指令数 CPI=(0.5a*2+0.2a*3+0.1a*4+0.2a*5)/a=3

  10. 初学Socket通信

    1.Socket:Socket就是套接字.客户端与服务器之间通信用的.Socket接口是TCP/IP网络的API. 2.SYN是TCP/IP建立连接时使用的握手信号.在客户端和服务器之间建立正常的TC ...