javascript solution share


  • 0
    Y
    var getPermutation = function(n, k) {
        var hash = [];
        var arr = [];
    
        var quotient = 0;
        var remainder = 0;
        var res = [];
        
        hash[0] = 1;
        hash[1] = 1;
        hash[2] = 2;
    
        for (let i = 3; i < 10; i++) {
        	hash[i] = i * hash[i-1];
        }
        for (let i = 0; i < n; i++) {
        	arr[i] = i + 1;
        }
        k = k-1;
        for (let i = 0; i < n; i++) {
        	quotient = Math.floor((k)/hash[n-i-1]);
        	remainder = (k)%hash[n-i-1];
        	k = remainder;
        	res.push(arr[quotient]);
        	delete arr[quotient];
        	arr.sort();
        }
        return res.join("");
    
    };
    

Log in to reply
 

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