public String getPermutation(int n, int k) {
        if (n < 1 || n > 9 || k <= 0) return null;
        ArrayList<Integer> nums = new ArrayList<Integer>();
        int fac = 1;
        for (int i = 1; i <= n; ++i) {
            nums.add(i);
            fac *= i;
        }

        fac /= n;
        k = k - 1; // start from 0

        String result = "";
        for (int i = n - 1; i >= 0; --i) {
            int index = k / fac;
            result += nums.get(index);
            nums.remove(index);
            k %= fac;
            if (i > 0) fac /= i;
        }
        return result;
    }