-
Notifications
You must be signed in to change notification settings - Fork 0
/
0078. Subsets.js
41 lines (36 loc) · 910 Bytes
/
0078. Subsets.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
// Given a set of distinct integers, nums, return all possible subsets (the power set).
// Note: The solution set must not contain duplicate subsets.
// Example:
// Input: nums = [1,2,3]
// Output:
// [
// [3],
// [1],
// [2],
// [1,2,3],
// [1,3],
// [2,3],
// [1,2],
// []
// ]
// 1) 回溯
/**
* @param {number[]} nums
* @return {number[][]}
*/
var subsets = function(nums) {
let list = []
const backtrack = (list, tempList, nums, start) => {
list.push([...tempList])
for (let i = start; i < nums.length; i++) {
tempList.push(nums[i])
backtrack(list, tempList, nums, i + 1)
tempList.pop()
}
}
nums = nums.sort((a, b) => a - b)
backtrack(list, [], nums, 0)
return list
}
// Runtime: 76 ms, faster than 10.87% of JavaScript online submissions for Subsets.
// Memory Usage: 36.3 MB, less than 5.88% of JavaScript online submissions for Subsets.