0ms C++ solutions


  • 0
    D
        class Solution {
    public:
        string getPermutation(int n, int k) {
            if(!n)
            {
                return "";
            }
            k--;
            string str = "";
            int result[n] = {0};
            int key[n] = {0};
            for(int i = 0; i < n; i++)
            {
                key[i] = i+1;
            }
            int temp = 1;
            for(int j = 1; j <= n; j++)
            {
                temp *= j;
            }
            vector<int> now(key, key+n);
            for(; n > 1; n--)
            {
                temp /= n;
                str += ('0'+now[k/temp]);
                now[k/temp] = 10;            
                sort(now.begin(), now.end(), less<int>());
                k = k % temp;            
            }
            str += ('0'+now[0]);
            return str;
        }
    };

Log in to reply
 

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