My 15ms Easy Solution


  • 0
    B
    public String getPermutation(int n, int k) {
    	StringBuilder sb = new StringBuilder();
    	int [] nums = new int [n];
    	int [] factorsVector = new int []{
    			-1,1,1,2,6,24,120,720,5040,40320,362880
    	};
    	for(int i=0;i<n;i++){
    		nums[i]=i+1;
    	}
    	k=k-1;
    	for(int i=n;i>=1;i--){
    		int num = k/factorsVector[i];
    		sb.append(nums[num]);
    		nums[num] = Integer.MAX_VALUE;
    		k = k%factorsVector[i];
    		Arrays.sort(nums);
    	}
    	return sb.toString();
    }

Log in to reply
 

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