Very Short and Simple Java Soln


  • 0
    V
    public class Solution {
        public String getPermutation(int n, int k) {
            double fact = 1;
            List<Integer> list = new LinkedList<>();
            for (int i = 1; i <= n; i++) {
                list.add(i);
                fact *= i;
            }
            StringBuilder sb = new StringBuilder();
            if (k-- > fact) sb.toString();
            while (! list.isEmpty()) {
                fact /= list.size();
                sb.append(list.remove((int) Math.floor(k/fact)));
                k %= fact;
            }
            return sb.toString();
        }
    }
    

Log in to reply
 

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