[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 ...
随机推荐
- h5页面乱码-设置编码
1.h5页面正常,重定向以后出现乱码,如图所示. 解决办法:重定向的时候 需要设置编码. 2.文件charset已经是utf-8,页面还是乱码,文件保存的格式也要是utf-8的哦
- 18 12 2 数据库 sql 的增删改查
---恢复内容开始--- 1 开始进入MySQL 的安装 https://www.cnblogs.com/ayyl/p/5978418.html 膜拜大神的博客 2 默认安装的时候 m ...
- hook截获自定义消息
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- Digital Twin——IoT的下一个浪潮
选自Medium 作者:Tomasz Kielar 京东云开发者社区编译 当前的商业格局受到了众多新技术的影响,有时确实让人很难能跟上技术迭新的速度.虽然很多人都熟悉工业4.0和物联网(IoT)这两个 ...
- SQL基础教程(第2版)第6章 函数、谓词、CASE表达式:练习题
END) AS low_price, END) AS mid_price, END) AS high_price FROM Product; 6_2.sql
- 最大连续子序列和,以及开始、结束下标(HDU 1003)
HDU1003 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the ...
- 安装lombok插件IDEA的插件栏加载不出来
打开 Setting-->Appearance & Behavior -->Syetem Setting -->Updates,将Use secure connection ...
- vue 动画框架Animate.css @keyframes
<script src="vue.js"></script> <link rel="stylesheet" href=" ...
- iOS中copy和=的区别
copy是浅拷贝即指针拷贝,让对象的引用计数加一 在MRC环境下,=只是简单的指针指向,引用对象的引用计数并不会增加,在用局部变量赋值时很容易出现野指针 在ARC环境下,=的效果等同于copy和ret ...
- Exit of “> ” mode in Unix shell
https://unix.stackexchange.com/questions/118209/exit-of-mode-in-unix-shell ^D will only work if a pr ...