[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 ...
随机推荐
- impdp导入.dmp到oracle
1.创建表空间 create tablespace CCGRP_PRO --表空间名 datafile 'D:\oracleData\test.dbf' --物理文件 表空间数据文件存放路径size ...
- 寒假day20
今天解决了部分信息爬取不下来的问题
- linux_c_udp_example
udp_server #include <stdlib.h> #include <string.h> #include <unistd.h> #include &l ...
- jsch通过SSH2执行linux命令
public class SSHUtils { private Channel channel; private Session session = null; private int timeout ...
- ..\OBJ\LED.axf: Error: L6218E: Undefined symbol EXTI_Init (referred from exti.o). 错误修改
今天在移植野火的程序到元子的开发平台上时候,发现自己在中断初话中断函数的时候出现了:..\OBJ\LED.axf: Error: L6218E: Undefined symbol EXTI_Init ...
- java8中的map 和reduce
map 1.使用map让集合里面的数字翻倍. List<Integer> numbers = Lists.newArrayList(1,2,3,4,5);List<Integer&g ...
- PAT Advanced 1086 Tree Traversals Again (25) [树的遍历]
题目 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For exam ...
- 和我一起从0学算法(C语言版)(一)
第一章 排序 第一节 简化版桶排法 友情提示:此文章分享给所有小白,大牛请绕路! 生活中很多地方需要使用排序,价格的由低到高.距离的由远及近等,都是排序问题的体现.如果排序量较少,依靠个人能力很容易实 ...
- springboot跨域请求接口示例
一.项目架构 二.项目内容 1.GlobalCrosConfig.java package com.config; import org.springframework.context.annotat ...
- epoll机制
一.参考网址 1.epoll机制:epoll_create.epoll_ctl.epoll_wait.close 2.Linux网络编程 使用epoll实现一个高性能TCP Echo服务器 3.用C写 ...