Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- string getPermutation(int n, int k) {
- string ans = "";
- vector<int>num;
- int fact[n+1];
- fact[0]=1;
- for(int i=1;i<=n;i++)
- {
- fact[i] = i * fact[i-1];
- num.push_back(i);
- }k--;
- while(n)
- {
- int id = k/(fact[n-1]);
- ans+=to_string(num[id]);
- num.erase(num.begin() + id);
- k%=fact[n-1];
- n--;
- }
- return ans;
- }
- };
Add Comment
Please, Sign In to add comment