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);//去除,为下一个结果做准备
        	}
        }
    }

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-11 22:25:42

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 index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum nu

leetCode 27.Remove Element (删除元素) 解题思路和方法

Remove Element Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 思路:此题和26题一脉相承,算法上不难,具体如代码所示: public class

leetCode 51.N-Queens (n皇后问题) 解题思路和方法

N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configur

leetCode 57.Insert Interval (插入区间) 解题思路和方法

Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], i

leetCode 22.Generate Parentheses (生成括号) 解题思路和方法

Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "

leetCode 39.Combination Sum(组合总和) 解题思路和方法

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includi

leetCode 86.Partition List(分区链表) 解题思路和方法

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example, Given 1->4->3->

leetCode 75.Sort Colors (颜色排序) 解题思路和方法

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl

leetCode 66.Plus One (+1问题) 解题思路和方法

Plus One Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 思路:给定一个数组,表示一个数,然后返回+1的值.主要就是进位的问题,代码如下: public class Solution { pu

leetCode 67.Add Binary (二进制加法) 解题思路和方法

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 思路:二进制加法,比较简单.代码如下: public class Solution { public String addBinary(String a, String b) { int len = Math.max(a.len