leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0060.cpp (524B)
0 class Solution { 1 public: 2 string getPermutation(int n, int k) { 3 vector<char> avail = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; 4 int factorial = 1, pos; 5 string res; 6 for (int i = 1; i <= n; i++) 7 factorial *= i; 8 for (k--; n; n--) { 9 factorial /= n; 10 res += avail[pos = k / factorial], k -= pos * factorial; 11 for (int i = pos; i < avail.size() - 1; i++) 12 avail[i] = avail[i + 1]; 13 } 14 return res; 15 } 16 };