zoukankan      html  css  js  c++  java
  • Subsets II

    1. Title

    Subsets II

    2.   Http address

    https://leetcode.com/problems/subsets-ii/

    3. The question

    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],
      []
    ]

    4. My code (AC)

     1 public static void helper(int [] nums, int start,List<Integer> sub_set, List<List<Integer>> res_set)
     2     {
     3         int len = nums.length;
     4         if ( start > len)
     5             return;
     6         res_set.add(new ArrayList<Integer>(sub_set));
     7         for(int i = start ; i < len; i++)
     8         {
     9             if ( i != start && nums[i] == nums[i-1])
    10                 continue;
    11             sub_set.add(nums[i]);
    12             helper(nums, i + 1, sub_set, res_set);
    13             sub_set.remove(sub_set.size() - 1);
    14         }
    15     }
    16     
    17     // Accepted
    18     public static List<List<Integer>> subsetsWithDup(int[] nums) {
    19         
    20         Arrays.sort(nums);
    21         
    22         List<List<Integer>> res_set = new ArrayList<List<Integer>>();
    23         helper(nums, 0, new ArrayList<Integer>(), res_set);
    24         return res_set;
    25     }
  • 相关阅读:
    mvn 创建的项目 导入到eclipse
    maven GroupID和ArtifactID
    eclipse配置maven + 创建maven项目
    微服务简介
    spring-boot5代码
    spring-boot5
    TextView及其子类
    RTMP协议
    实现输出h264直播流的rtmp服务器
    Android按键事件传递流程(二)
  • 原文地址:https://www.cnblogs.com/ordili/p/4928331.html
Copyright © 2011-2022 走看看