[leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
题意:
是的[leetcode]78. Subsets数组子集 follow up
这题强调给定数组可以有重复元素
思路:
需要先对给定数组排序,以便去重
代码:
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
if(nums == null || nums.length ==0 )return result;
Arrays.sort(nums);
List<Integer> path = new ArrayList<>();
dfs(0, nums, path, result);
return result;
}
private void dfs(int index, int[] nums, List<Integer> path, List<List<Integer>> result){
result.add(new ArrayList<>(path));
for(int i = index; i < nums.length; i++){
if( i != index && nums[i] == nums[i-1] ) continue;
path.add(nums[i]);
dfs(i + 1, nums, path, result);
path.remove(path.size() - 1);
}
}
}
[leetcode]90. Subsets II数组子集(有重)的更多相关文章
- LeetCode 90. Subsets II (子集合之二)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- leetCode 90.Subsets II(子集II) 解题思路和方法
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [LeetCode] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [LeetCode] 90. Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- Leetcode#90 Subsets II
原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- LeetCode Problem 90. Subsets II
python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
随机推荐
- gitlab部署步骤+汉化
系统环境centos7 下载gitlab安装包 https://packages.gitlab.com/gitlab/gitlab-ce 我下载的版本是 gitlab-ce-11.1.4-ce.0.e ...
- Web前端新手想提升自身岗位竞争力,需做好这3件事!
Web前端开发行业的发展前景毋庸置疑,只要是互联网企业,几乎都需要Web前端开发工程师.虽然Web前端入行门槛低,但竞争逐渐激烈,想要取得高薪,就一定要具备强大的实力.那么,在重庆Web前端培训学习中 ...
- WIFI 802.11 a/b/g/n/ac
802.11 a/b/g/n/ac FHSS: Frequency-hopping spread spectrum (FHSS) is a method of transmitting radio s ...
- because there was insufficient free space available after evicting expired cache entries
Tomcat运行的时候哗哗哗的报警告 版本是Tomcat 8.5.15 告警信息关键字如下 because there was insufficient free space available af ...
- golang http proxy反向代理
本文介绍golang中如何进行反向代理. 下面例子中, proxy server接收client 的 http request,转发给true server,并把 true server的返回结果再发 ...
- win10+vs2015编译caffe的cpu debug版本、部署matcaffe
一.编译caffe 1.安装python-3.5.2-amd64.exe https://www.python.org/ftp/python/3.5.2/python-3.5.2-amd64.exe ...
- PAT 乙级 1065 单身狗 (25 分)
1065 单身狗 (25 分) “单身狗”是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数 N(≤ 50 000),是 ...
- java内存区域之程序计数器
程序计数器(program counter register) 作用:字节码解释其工作时,通过这个计数器的值的改变,来选取下一条执行的字节码命令. 由于java虚拟机的都线程是通过线程轮流切换,并分配 ...
- debian系统的另一个包管理器aptitude
最近在玩OPI的时候遇到了一个包因为各种依赖问题死活装不上.苦搜很久找到这个aptitude包管理器. 基于debain衍生的各种操作系统比较常用包管理器是apt,不过apt在遇到依赖问题的时候处理得 ...
- C语言数据结构基础学习笔记——栈和队列
之前我们学过了普通的线性表,接下来我们来了解一下两种特殊的线性表——栈和队列. 栈是只允许在一端进行插入或删除的线性表. 栈的顺序存储结构也叫作顺序栈,对于栈顶指针top,当栈为空栈时,top=-1: ...