leetCode 90.Subsets II(子集II) 解题思路和方法
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,2],
a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
思路:这一题比subsets多一了一道反复,详细代码例如以下:
public class Solution {
boolean[] b;
Set<String> set;
List<List<Integer>> list;
Set<String> set1;
public List<List<Integer>> subsetsWithDup(int[] nums) {
b = new boolean[nums.length];
set = new HashSet<String>();
list = new ArrayList<List<Integer>>();
set1 = new HashSet<String>();
Arrays.sort(nums);
count(nums,"",nums.length,0);
return list;
}
private void count(int[] nums,String s,int n,int j){
//没有反复才加入
if(set.add(s)){
//以","切割数组
String[] sa = s.split(",");
List<Integer> al = new ArrayList<Integer>();
for(int i = 0; i < sa.length; i++){
if(sa[i].length() > 0){
al.add(Integer.parseInt(sa[i]));
}
}
Collections.sort(al);
if(set1.add(al.toString()))
list.add(al);
}
for(int i = j; i < nums.length;i++){
if(!b[i]){
b[i] = true;
count(nums,s + "," + nums[i],n-1,i+1);
b[i] = false;
}
}
}
}
以下这样的写法更简洁:
public class Solution {
List<List<Integer>> list;//结果集
List<Integer> al;//每一项
public List<List<Integer>> subsetsWithDup(int[] nums) {
list = new ArrayList<List<Integer>>();
al = new ArrayList<Integer>();
Arrays.sort(nums);
//排列组合
count(nums,al,0);
return list;
}
private void count(int[] nums,List<Integer> al,int j){
list.add(new ArrayList<Integer>(al));//不反复的才加入
for(int i = j; i < nums.length;i++){
if(i == j || nums[i] != nums[i-1]){//去除反复
al.add(nums[i]);//加入
count(nums,al,i+1);
al.remove(al.size()-1);//去除。为下一个结果做准备
}
}
}
}
leetCode 90.Subsets II(子集II) 解题思路和方法的更多相关文章
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- leetcode 113. Path Sum II (路径和) 解题思路和方法
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- leetCode 86.Partition List(分区链表) 解题思路和方法
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- leetCode 75.Sort Colors (颜色排序) 解题思路和方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- leetCode 15. 3Sum (3数之和) 解题思路和方法
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- leetCode 61.Rotate List (旋转链表) 解题思路和方法
Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For ex ...
- leetCode 67.Add Binary (二进制加法) 解题思路和方法
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- leetCode 66.Plus One (+1问题) 解题思路和方法
Plus One Given a non-negative number represented as an array of digits, plus one to the number. The ...
随机推荐
- Python学习杂记_5_列表常用操作
列表操作 列表时用方括号括起来的一组元素值,是可变变量,可通过下表取值,也可以通过下表来修改值,列表中的元素是有序的,可以是不同的基本数据类型,如: names=[1, 2, 3, “abc”, “d ...
- sql查询 ,多行数据合并成一行,并且显示合并后某一列的值拼接结果
select [value] = stuff((select ','+modmb003 from modmb detail where modmb=18 for xml path('')), 1, ...
- windows下pip安装python module失败
C:\Python27\pywin32-214>setup.py -q install Building pywin32 2.7.214.0 Traceback (most recent cal ...
- hdu 4859(思路题)
Goffi and Squary Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- GRDB自定义的纯函数
GRDB自定义的纯函数 在GRDB中,用户可以自定义SQlite函数.这样,在SQL语句中,可以直接调用这些函数.但是在定义的时候,用户需要指定函数的pure属性,表示该函数是否为纯函数.纯函数是 ...
- POJ 2411 Mondriaan's Dream [经典状态压缩dp]
题意:略. 思路:这一题开始做的时候完全没有思路,便去看了别人的题解. 首先,对于这个题目解法想有一个初步的了解,请看这里:http://www.2cto.com/kf/201208/146894.h ...
- 某考试 T1 str
一开始死磕sam,发现根本没法做...... 后来想了想,反正匹配子串的大部分不是sam就是 二分+hash啊,,,于是就想了想二分+hash,发现好像可以做啊! 就是假设我们要让 s1[1] 映射到 ...
- java visual VM使用简介
转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/44999175 作者:小马 VisualVM 是一款免费的性能分析工具.它通过 jv ...
- iscroll 子表左右滚动同时保持页面整体上下滚动
if ( this.options.preventDefault && !utils.isBadAndroid && !utils.preventDefaultExce ...
- Flutter接入极光推送
(1)搜索 https://pub.dartlang.org/packages/jpush_flutter ,安装插件,并且按照官方配置 /android/app/build.gradle andro ...