My easy understanding AC java solution


  • -1
    D
    public class Solution {
                   public String getPermutation(int n, int k) {
            String res="";	      
            int[] factor=new int[n+1];
            Arrays.fill(factor, 1);
    	       for(int i=n;i>=1;i--){
    	    	   for(int j=1;j<=i;j++){
    	    		   factor[i]*=j;
    	    	   }	    	   
    	       }
    	    List<Integer> list=new ArrayList<Integer>();
    	    int remains=1;
    	    while(remains<=n){
    	    	list.add(remains);
    	    	remains++;
    	    }
    	    for(int i=n;i>1;i--){
    	    	res+=list.get((k-1)/factor[i-1]);
    	    	list.remove((k-1)/factor[i-1]);
    	    	k=(k-1)%factor[i-1]+1;
    	    }
    	    if(!list.isEmpty()) res+=list.get(0);
            return res;
        }
    

    }


Log in to reply
 

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