Backtracking Javascript


  • 0
    S
    /**
     * @param {number[]} nums
     * @return {number[][]}
     */
    var subsets = function(nums) {
        let chosen = [];
        let subSets = [];
        findSubsetsHelper(nums, chosen, subSets);
        return subSets;
    };
    
    var findSubsetsHelper = function(set, chosen, subSets) {
        if(set.length === 0) {
            subSets.push(Object.assign([],chosen));
        }
        else {
            let pivot = set[0];
            set.splice(0, 1);
            chosen.push(pivot);
            findSubsetsHelper(set, chosen, subSets);
    
            chosen.pop();
            findSubsetsHelper(set, chosen, subSets);
    
            set.splice(0, 0, pivot);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.