Skip to content

Latest commit

 

History

History
19 lines (19 loc) · 491 Bytes

subsets.md

File metadata and controls

19 lines (19 loc) · 491 Bytes
/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var subsets = function(nums) {
    let result = [];
    function backtrack(numbers, startIndex, cur) {
        result.push([...cur]) // 保存结果
        for (let i = startIndex; i < numbers.length; i++) {
            cur.push(numbers[i]);
            backtrack(numbers, i + 1, cur); // 注意下一次决策的起始位置
            cur.pop();
        }
    }
    backtrack(nums, 0, []);
    return result;
};