-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
29 lines (23 loc) · 863 Bytes
/
index.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
/**
* @param {number[]} nums
* @return {number[][]}
*/
// Runtime: 72 ms, faster than 91.85% of JavaScript online submissions for Subsets.
// Memory Usage: 40.7 MB, less than 85.06% of JavaScript online submissions for Subsets.
// https://leetcode.com/problems/subsets/discuss/27281/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)
// backtrack
var subsets = function (nums) {
const backtrack = (list, tempList, nums, start) => {
list.push(tempList.slice())
for (let i = start; i < nums.length; i++) {
tempList.push(nums[i]);
backtrack(list, tempList, nums, i + 1);
tempList.pop();
}
};
const list = [];
nums.sort((a, b) => a - b);
backtrack(list, [], nums, 0);
return list;
};
export default subsets;