leetcode — subsets-ii
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的更多相关文章
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- [LeetCode] Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [leetcode]Subsets II @ Python
原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might cont ...
- [Leetcode] Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [LeetCode] Subsets II [32]
题目 Given a collection of integers that might contain duplicates, S, return all possible subsets. Not ...
- [Leetcode] subsets ii 求数组所有的子集
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [LeetCode]Subsets II生成组合序列
class Solution {//生成全部[不反复]的组合.生成组合仅仅要採用递归,由序列从前往后遍历就可以. 至于去重,依据分析相应的递归树可知.同一个父节点出来的两个分支不能一样(即不能与前一个 ...
- LeetCode:Subsets I II
求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...
- Subsets II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Subsets II - LeetCode 注意点 有重复的数字 数组可能是无序的,要先排序 解法 解法一:递归,只需要在Subsets中递归写法的基础上 ...
- 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 ...
随机推荐
- 数据分析 大数据之路 三 numpy
import numpy as np a = np.arange(9) b = a.reshape(3,3) print(b) print(b.max(axis=0)) # axis=0 示为 Y 轴 ...
- 阿里云消息队列(MQ)服务
A.首先在阿里云上申请消息队列MQ服务: B.然后创建一个Topic(主题,一级主题):然后创建生产者与消费者: C.不过此时还没有结束 ,还需要创建一个AccessKey和AccessSecret( ...
- 【安全性测试】利用反编译查看对应activity的方法采用hook技术绑定劫持_入门
本次主要为了研究手机端的安全性而写的一篇文章,在基于自己对手机安全性的研究下,想到了这些工具之间的结合,当然这也算是第一次对手机安全研究勇敢地踏出一步,也不知道是否成功,还是准备撞南墙撞到底吧! 使用 ...
- php 将图片转成base64
/** * 获取图片的Base64编码(不支持url) * @date 2017-02-20 19:41:22 * * @param $img_file 传入本地图片地址 * * @return st ...
- iOS 开发中单元格cell高度自适应
高度自适应分下面两种情况 1.用代码自定义的cell 用代码自定义的cell,cell高度自定义需要我们手动的去计算每个cell的字符串高度.然后返回对应的高度即可. 2.用XIB 或者 StoreB ...
- js方法实现--上传文件功能
function createUploadForm(fileElementId, data, curFileList) { var id = new Date().getTime(); var for ...
- DevOps详解
最近我阅读了很多有关DevOps的文章,其中一些非常有趣,然而一些内容也很欠考虑.貌似很多人越来越坚定地在DevOps与chef.puppet或Docker容器的熟练运用方面划了等号.对此我有不同看法 ...
- JUnit介绍(转)
测试的重要性毋庸再说,但如何使测试更加准确和全面,并且独立于项目之外并且避免硬编码,JUnit给了我们一个很好的解决方案.一.引子 首先假设有一个项目类SimpleObject如下: pu ...
- 大数相加 Big Num
代码: #include<stdio.h>#include<algorithm>#include<iostream>#include<string.h> ...
- python制作词云
需要模块wordcloud,pip install wordcloud安装即可.代码: , #边距background_color='black',#指定背景颜色font_path='simhei.t ...