zoukankan      html  css  js  c++  java
  • leetcode------Subsets II

    标题: Subsets II
    通过率: 27.5
    难度: 中等

    Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2], a solution is:

    [
      [2],
      [1],
      [1,2,2],
      [2,2],
      [1,2],
      []
    ]

    从第一个版本中可以看出来,这里需要增加相同判断。具体看代码

     1 public class Solution {
     2     public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) {
     3         ArrayList<ArrayList<Integer>> res=new ArrayList<ArrayList<Integer>>();
     4         ArrayList<Integer> tmp=new ArrayList<Integer>();
     5         Arrays.sort(num);
     6         dfs(res,tmp,0,num);
     7         return res;
     8     }
     9     public void dfs(ArrayList<ArrayList<Integer>> res,ArrayList<Integer> tmp,int start,int[] num){
    10         if(!res.contains(tmp))
    11             res.add(new ArrayList<Integer>(tmp));
    12         for(int i=start;i<num.length;i++){
    13             tmp.add(num[i]);
    14             dfs(res,tmp,i+1,num);
    15             tmp.remove(tmp.size()-1);
    16         }
    17     }
    18 }
  • 相关阅读:
    原型设计 + 用户规格说明书
    第三次作业
    MathExam第二次作业
    第一次随笔
    冲鸭第一的合作
    功能规格说明书
    测试与优化
    结对编程
    高分小学计算器
    现实与梦
  • 原文地址:https://www.cnblogs.com/pkuYang/p/4342786.html
Copyright © 2011-2022 走看看