[LC] 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.
Example:
Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
] Solution 1
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length == 0) {
return res;
}
Arrays.sort(nums);
helper(res, new ArrayList<>(), nums, 0);
return res;
} private void helper(List<List<Integer>> res, List<Integer> list, int[] nums, int index) {
res.add(new ArrayList<>(list));
for (int i = index; i < nums.length; i++) {
if (i > index && nums[i] == nums[i - 1]) {
continue;
}
list.add(nums[i]);
// use i not index b/c index smaller than i
helper(res, list, nums, i + 1);
list.remove(list.size() - 1);
}
}
}
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
if (nums == null) {
return result;
}
List<Integer> list = new ArrayList<>();
Arrays.sort(nums);
helper(nums, list, 0, result);
return result;
} private void helper(int[] nums, List<Integer> list, int level, List<List<Integer>> result) {
if (level == nums.length) {
result.add(new ArrayList<>(list));
return;
}
// need to add first, skip index and then remove
list.add(nums[level]);
helper(nums, list, level + 1, result);
list.remove(list.size() - 1); while (level + 1 < nums.length && nums[level] == nums[level + 1]) {
level += 1;
}
helper(nums, list, level + 1, result);
}
}
[LC] 90. Subsets II的更多相关文章
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- 90. Subsets II
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning
78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【LeetCode】90.Subsets II
Subsets II Given a collection of integers that might contain duplicates, nums, return all possible s ...
- LeetCode Problem 90. Subsets II
python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...
- 78. Subsets 90. Subsets II
1. Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...
- 90. Subsets II (Back-Track, DP)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- Leetcode#90 Subsets II
原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...
随机推荐
- js动态删除行错误
Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'. js ...
- 理解python中的'*','*args','**','**kwargs'
本文来源:http://blog.csdn.net/callinglove/article/details/45483097 让我们通过以下6步来理解: 1. 通过一个函数调用来理解’*’的作用 2. ...
- URL&HTTP协议详解
本文来自公开课笔记,主要做知识的记录,谢谢! ·接口测试核心技术--URL&HTTP协议详解. ·URL: 统一资源定位符. 示例: https://ke.qq.com/course/3157 ...
- 关于typedef的一些小知识
//关于typedef //1.在c语言中定义一个结构体typedef struct student{ int a;}stu;//typedef 给结构体起了个别名 stu;//于是,在声明变量的时候 ...
- CTF-域渗透--HTTP服务--命令注入2
开门见山 1. 启动metasploit 2. 设置参数参数选项 3. 查看最后设置后的结果 4. 启动监听 5. 使用msfvemon制作webshell 6. 开启apache服务 7. 使用ba ...
- UVA 11732 链表+字典树
因为字符集比较大,所以就不能用简单字典树,在字典树里面,用链表进行存储.这个倒是不难,练了下手 统计的时候还是有点难搞,因为要算所有的两两比较的次数之和,对分叉处进行计算,注意细节 #include ...
- (函数)P1149 火柴棒等式
题解: #include<stdio.h>int a[10]={6,2,5,5,4,5,6,3,7,6};int num(int n){ ...
- 机器学习分布式框架horovod安装 (Linux环境)
1.openmi 下载安装 下载连接: https://download.open-mpi.org/release/open-mpi/v4.0/openmpi-4.0.1.tar.gz 安装命令 1 ...
- php抓取网站图片源码
<?php /*完成网页内容捕获功能*/ function get_img_url($site_name){ $site_fd = fopen($site_name, "r&q ...
- MobX 在 hook 中的使用
关于 mobX 在 react 16.8.0 以上的用法 以下例子均取自官网文档 一般用法: import { observer, useLocalStore } from 'mobx-react'; ...