Pretty simple java solution


  • 0

    public class Solution {

    public String getPermutation(int n, int k) {
         String ret="";
        int num=1;
        List<Integer> arr = new ArrayList<Integer>();
        for(int i=1;i<=n;i++)
        {
            num*=i;
            arr.add(i);
        }
        k--;
         while(arr.size()!=0)
        {
         num/=(arr.size());
         ret+=arr.remove(k/num);
         k-=(k/num)*num;
         }
        return ret;
    }
    

    }


  • 0
    H

    k %= num; is more clear. And using LinkedList and StringBuilder should be more efficient.


Log in to reply
 

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