4 lines cheating C++ because 9! iterations are pretty fast.


  • 0

    This solution gets accepted because 9! is < 1M and runs in less than a second on most PCs.

        string getPermutation(int n, int k) {
            string s;
            for (int j = 1; j <= n; ++j) s.push_back(j + '0');
            while (--k) next_permutation(s.begin(), s.end());
            return s;
        }
    

Log in to reply
 

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