90. Subsets II (Java)
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],
[]
]
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
result = new ArrayList<>();
List<Integer> ans = new ArrayList<Integer>();
Arrays.sort(nums); //nums可能是乱序的,要先排序
backtrack(ans, nums, 0);
return result;
}
public void backtrack(List<Integer> ans, int[] nums, int depth){
if(depth >= nums.length) {
List<Integer> new_ans = new ArrayList<Integer>(ans);
result.add(new_ans);
return;
}
int i = depth+1;
while(i < nums.length){
if(nums[depth] == nums[i]) i++;
else break;
}
int j = depth;
backtrack(ans, nums, i); //not add
while(j < i){
ans.add(nums[depth]);
backtrack(ans, nums, i);
j++;
}
//reset
while(j > depth){
ans.remove(ans.size()-1);
j--;
}
}
private List<List<Integer>> result;
}
90. Subsets II (Java)的更多相关文章
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- LeetCode Problem 90. Subsets II
python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...
- 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 90. Subsets II (子集合之二)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- 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 ...
随机推荐
- GitHub-Microsoft:sql-server-samples
ylbtech-GitHub-Microsoft:sql-server-samples 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 1. https://gi ...
- python汉字编解码问题
http://www.cnblogs.com/rollenholt/archive/2011/08/01/2123889.html
- mariadb数据库(2)增删改与 单表查询
一.数据类型 MariaDB数据类型可以分为数字,日期和时间以及字符串值. 使用数据类型的原则:够用就行, 尽量使用范围小的,而不用大的 常用的数据类型 整数:int, bit 小数:decimal ...
- iOS10权限问题
下图就是Info.plist的常用的权限问题: * 麦克风权限:Privacy - Microphone Usage Description 是否允许此App使用你的麦克风? * 相机权限: Priv ...
- java:nginx(java代码操作ftp服务器)
1.检查是否安装了vsftpd [root@linux01 ~]# rpm -qa|grep vsftpd 2.安装vsftpd [root@linux01 ~]# yum -y install vs ...
- vue中refs的使用
最近在看其他项目的过程中,发现在dom节点上使用了ref="xxx"的使用,以前一直不知道该属性起着什么作用,因为一直忙着写项目. 这两天项目不忙了,有闲心来看别人做的项目了,就看 ...
- 利用python将excel数据解析成json格式
利用python将excel数据解析成json格式 转成json方便项目中用post请求推送数据自定义数据,也方便测试: import xlrdimport jsonimport requests d ...
- PI膜概述
一.概述 1.简述 聚酞亚胺薄膜又称PI薄膜(polyimide filin)是一种含有酞亚胺或丁二酞亚胺的绝缘类高分子材料.是目前工程塑料中耐热性最好的品种之一. 2.发展简史 1908年,PI聚合 ...
- 【VS开发】CTimeSpan类
CTimeSpan类. 日期和时间类简介 CTime类的对象表示的时间是基于格林威治标准时间(GMT)的.CTimeSpan类的对象表示的是时间间隔. CTi ...
- 【Windows】Windows server2008远程桌面只允许同时存在一个会话
打开控制面板-管理工具,终端服务-终端服务配置 1.连接:RDP-tcp 点右键,属性.网络适配器-最大连接数,只允许1个. 2.终端服务器授权模式:点右键,属性.常规,限制每个用户只能使用一个会话, ...