Leetcode 60. Permutation Sequence | | Leetcode 60. Permutation Sequence 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 1. Description 2. Solution1234567891011121314151617181920212223242526272829class Solution {public: string getPermutation(int n, int k) { string result; int factorial[n + 1] = {0}; factorial[0] = 1; for(int i = 1; i <= n; i++) { factorial[i] = factorial[i - 1] * i; } vector<int> nums; for(int i = 1; i <=n; i++) { nums.push_back(i); } for(int i = n; i > 0; i--) { int remainder = k % factorial[i - 1]; int quotient = k / factorial[i - 1]; if(remainder) { quotient++; } result += to_string(nums[quotient - 1]); nums.erase(nums.begin() + quotient - 1); k %= factorial[i - 1]; if(k == 0) { k = factorial[i - 1]; } } return result; }}; Reference https://leetcode.com/problems/permutation-sequence/description/ 如果有收获,可以请我喝杯咖啡! 赏 微信打赏 支付宝打赏