• [LeetCode][JavaScript]Subsets II


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

    https://leetcode.com/problemset/algorithms/


    上一题相同的思路,加上去重就可以了。

    http://www.cnblogs.com/Liok3187/p/4733079.html

    非递归:

     1 /**
     2  * @param {number[]} nums
     3  * @return {number[][]}
     4  */
     5 var subsetsWithDup = function(nums) {
     6     nums = nums.sort(sorting);
     7     var res = [[]], arr = [], visited = new Set();
     8     for(var i = 0; i < nums.length; i++){
     9         if(!visited.has(nums[i])){
    10             res.push([nums[i]]);
    11             visited.add(nums[i]);
    12         }
    13         arr.push({val : [nums[i]], pos : i});
    14     }
    15     getSets(arr);
    16     return res;
    17 
    18     function getSets(arr){
    19         var i, j, tmp, nextArr = [];
    20         for(i = 0; i < arr.length; i++){
    21             for(j = arr[i].pos + 1; j < nums.length; j++){
    22                 tmp = arr[i].val.slice(0);
    23                 tmp.push(nums[j]);
    24                 if(!visited.has(tmp.join('#'))){
    25                     res.push(tmp);
    26                     visited.add(tmp.join('#'));
    27                 }
    28                 nextArr.push({val : tmp, pos : j});
    29             }
    30         }
    31         if(nextArr.length > 0){
    32             getSets(nextArr);
    33         }
    34     }
    35 
    36     function sorting(a, b){
    37         return a - b;
    38     }
    39 };

    递归:

     1 /**
     2  * @param {number[]} nums
     3  * @return {number[][]}
     4  */
     5 var subsetsWithDup = function(nums) {
     6     nums = nums.sort(sorting), visited = new Set();
     7     var res = [];
     8     for(var i = 0; i < Math.pow(2, nums.length); i++){
     9         var str = padLeft(i, nums.length);
    10         var tmp = [];
    11         for(var j = 0; j < str.length; j++){
    12             if(str[j] === '1'){
    13                 tmp.push(nums[j]);
    14             }
    15         }
    16         if(!visited.has(tmp.join('#'))){
    17             res.push(tmp);
    18             visited.add(tmp.join('#'));
    19         }
    20     }
    21     return res;
    22     
    23     function padLeft(num, len){
    24         var res = "", i = len;
    25         while(i--) res += '0';
    26         var tmp = parseInt(num).toString(2);
    27         res = res + tmp;
    28         return res.substring(tmp.length, res.length);
    29     }
    30 
    31     function sorting(a, b){
    32         return a -b;
    33     }
    34 };
  • 相关阅读:
    Qt技巧、常用第三方库包含(qmake的.pro文件、CMakeLists.txt文件)
    STL资源
    debian 9 安装node angular
    debian 9 安装jenkins
    Consul 入门操作
    Docker 部署 postgresql 与 pgadmin4
    Docker File 与 Docker Compose
    Centos jdk
    Angular7 路由
    Centos Supervisor
  • 原文地址:https://www.cnblogs.com/Liok3187/p/4733082.html
Copyright © 2020-2023  润新知