-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path90.子集-ii.js
48 lines (40 loc) · 993 Bytes
/
90.子集-ii.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* @version: v 1.0.0
* @Github: https://github.com/GitHubGanKai
* @Author: GitHubGanKai
* @Date: 2021-01-04 13:50:34
* @LastEditors: gankai
* @LastEditTime: 2021-01-04 13:50:52
* @FilePath: /ReactDemo/Users/mac/Desktop/gankai/leetcode/90.子集-ii.js
*/
/*
* @lc app=leetcode.cn id=90 lang=javascript
*
* [90] 子集 II
*/
// @lc code=start
/**
* @param {number[]} nums
* @return {number[][]}
*/
var subsetsWithDup = function(nums) {
nums = nums.sort((a,b) => a-b);
const res = [];
function fn(length, start=0, arr = []) {
if (arr.length === length) {
res.push(arr.slice());
return;
}
for(let i=start; i<nums.length; i++) {
if (i !== start && nums[i-1] === nums[i]) continue;
arr.push(nums[i]);
fn(length, i+1, arr);
arr.pop();
}
}
for(let length=0; length<=nums.length; length++) {
fn(length);
}
return res;
};
// @lc code=end