Problem 60: Permutation Sequence

https://leetcode.com/problems/permutation-sequence/

思路

public class Solution {
    public String getPermutation(int n, int k) {
        LinkedList<Integer> list = new LinkedList<Integer>();
        for (int i = 1; i <= n; i++) {
            list.add(i);
        }
        int fact = 1;
        for (int i = 2; i <= n; i++) {
            fact *= i;
        }

        StringBuilder sb = new StringBuilder();
        for (k--; n > 0; n--) {
            fact /= n;
            sb.append(list.remove(k / fact));
            k %= fact;
        }

        return sb.toString();

    }
}

易错点

  1. 用 LinkedList 把元素存好,然后一个一个地取出来

  2. for 循环 k-- 相当于是 k = k - 1;

    for (k--; n > 0; n--) {
           fact /= n;
           sb.append(list.remove(k / fact));
           k %= fact;
    }

Last updated