leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0060.cpp (524B)


      1 class Solution {
      2   public:
      3     string getPermutation(int n, int k) {
      4         vector<char> avail = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
      5         int factorial = 1, pos;
      6         string res;
      7         for (int i = 1; i <= n; i++)
      8             factorial *= i;
      9         for (k--; n; n--) {
     10             factorial /= n;
     11             res += avail[pos = k / factorial], k -= pos * factorial;
     12             for (int i = pos; i < avail.size() - 1; i++)
     13                 avail[i] = avail[i + 1];
     14         }
     15         return res;
     16     }
     17 };