My Java Solution


  • 0
    M

    public class Solution {
    public String getPermutation(int n, int k)
    {
    int []num = new int[n];
    int permCount = 1;
    for(int i=0;i<n;i++)
    {
    num[i] = i+1;
    permCount*=(i+1);
    }
    k--; //康托编码从零开始 原来的第K个就是第K-1个

    	StringBuilder target = new StringBuilder();  
    	
    	for(int i=0;i<n;i++)
    	{  
    		permCount = permCount/(n-i);
    		int choosed = k/permCount; 
    		target.append(num[choosed]); 
    		
    		for(int j=choosed;j<n-i-1;j++)
    		{  
    			num[j] = num[j+1];  
    		}  
    		k = k%permCount; //在每组中数字的位置 
    	}  
    	
    	return target.toString();  
    }  
    

    }


Log in to reply
 

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