新博文地址:[leetcode]Subsets II
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],
[]
]
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],
[]
]
很典型的递归,与Subsets区别的是,这道题,s是包含重复元素的,其实好像木有太大影响,只需要用一个set集合在过滤掉重复的list即可。
List<List<Integer>> result = new ArrayList<List<Integer>>(); Set<List<Integer>> resultSet = new HashSet<List<Integer>>(); public List<List<Integer>> subsetsWithDup(int[] num) { List<Integer> nil = new ArrayList<Integer>(); result.add(nil); if(num == null || num.length == 0){ return result; } Arrays.sort(num); List<Integer> list =new ArrayList<Integer>(); for(int i = 0 ; i < num.length; i++){ list.add(num[i]); getSubSet(num, i, list); list.remove(list.size() - 1); } return result; } private void getSubSet(int[] num,int n,List<Integer> list){ if(n < num.length){ if(!resultSet.contains(list)){ List<Integer> copy = new ArrayList<Integer>(list); result.add(copy); resultSet.add(copy); } } for(int i = n + 1; i < num.length; i++){ list.add(num[i]); getSubSet(num,i,list); list.remove(list.size() - 1); } }
相关推荐
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
大佬的leetcode刷题笔记(c++版本)
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101_源码.zip
vs code LeetCode 插件
leetcode中文版
terminal-leetcode, 终端Leetcode是基于终端的Leetcode网站查看器 终端 leetcode终端leetcode是基于终端的leetcode网站查看器。本项目是由 RTV 激发的。 我最近正在学习本地化的反应,以实践我的新知识,并根据这个...
100个leetCode详细解答
leetcode刷题, 直接用leetcode的分类方式.
LeetCode 刷题汇总1
LeetCode 选讲1
该分类为结合《算法导论》的内容,给出Leetcode题目分类。题目主要集中在Leetcode的前400题中,也包括有后面的一些经典值得刷的题。该题目分类按照算法和数据结构排版,即可供单独Leetcode刷题使用,也可以配合学习...
这份文档列出了leetcode几乎所有题目(大约134题)的分类以及难度指示,是刷leetcode的必备良品。现在leetcode总的题目数已经达到150题,所以有部分题目没有包含在这个文档中,但是——足够了。po主刷leetcode的时候...
LeetCode 刷题笔记
(C++)LeetCode刷题题解答案
leetcode高频面试笔试题150+道,亲身总结。
LeetCode面试笔试题
LeetCode 刷题
leetcode全套解答python版本。包括更新到10月份的的leetcode
LeetCode-Swift, 快速LeetCode解决方案 LeetCodeLeetCode在线判断是一个包含很多收费算法的网站。 them Google Google Google Google LinkedIn this this repo 。 请免费参考并收费STAR以支持这个 repo,