JavaScript concise solution


  • 0
    D
    /**
     * @param {number} n
     * @param {number} k
     * @return {number[][]}
     */
    var combine = function(n, k) {
        
        let memoryMap = [];
    
        combineUtil([], 1);
      
        function combineUtil(arr, start){
            if(arr.length == k){
              memoryMap.push([...arr]);
              return;
            }
            
            for(let i = start; i <= n; i++){
                arr.push(i);
                combineUtil(arr, i + 1);
                arr.pop();
            }
        }
      
        return memoryMap;
    };
    
    
    
    

Log in to reply
 

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