leetcode-----60. 第k个排列

代码

class Solution {
public:
    string getPermutation(int n, int k) {
        string ans;
        vector<bool> st(10);
        for (int i = 0; i < n; ++i) {
            int fact = 1;
            for (int j = 1; j <= n - i - 1; ++j) fact *= j;

            for (int j = 1; j <= n; ++j) {
                if (!st[j]) {
                    if (fact < k) k -= fact;
                    else {
                        ans += to_string(j);
                        st[j] = true;
                        break;
                    }
                }
            }
        }
        return ans;
    }
};
原文地址:https://www.cnblogs.com/clown9804/p/13262340.html