90. Subsets II(js)

90. Subsets II

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: [1,2,2]
Output:
[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]
题意:求数组所有不重复子数组
代码如下:
/**
 * @param {number[]} nums
 * @return {number[][]}
 */
//回溯
var subsetsWithDup = function(nums) {
        nums.sort((a,b)=>a-b);
        let res=[];
        let cur=[]
        backtrack(res,cur,0,nums);
        return res;
    }
var backtrack =function(res,curr,index,nums){
        res.push(curr);
        for(let i=index;i<nums.length;i++){
            if(i>index && nums[i-1]==nums[i]) continue;
            backtrack(res,[...curr,nums[i]],i+1,nums);
        }
    }

 

posted @ 2019-04-14 21:14  mingL  阅读(133)  评论(0编辑  收藏  举报