-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubSets.js
35 lines (34 loc) · 940 Bytes
/
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
// 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],
// []
// ]
/**
* @param nums: A set of numbers
* @return: A list of lists
*/
const subsetHelper = (nums, subset, result, startIndex) => {
result.push(subset.slice(0)) // 创建副本,及时捕获值。否则最后得到 m 个相同的[]。作用域的问题。
for (let i = startIndex; i < nums.length; i++) {
subset.push(nums[i])
subsetHelper(nums, subset, result, i + 1)
subset.pop()
}
}
const subsets = function (nums) {
if (!nums) return []
if (nums.length === 0) return [[]]
let result = []
subsetHelper(nums.sort((a, b) => (a - b)), [], result, 0) // sort() 会按 ASKII 来排,2 > 10
return result
}