Clean c++ solution


  • 0
    string getPermutation(int n, int k) {
        vector<int> used(n);
        k--;
        int total = 1;
        for (int i = 1; i <= n; i++)
            total *= i;
        string result(n, ' ');
        for (int i = 0; i < n; i++) {
            total /= n - i;
            int index = k / total, j = 0;
            k %= total;
            while (used[j] == 1 || index--)
                j++;
            used[j] = 1;
            result[i] = '1' + j;
        }
        return result;
    }
    

Log in to reply
 

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