首页 > 代码库 > LeetCode--Permutation Sequence

LeetCode--Permutation Sequence

 

 1 class Solution { 2 public: 3     string getPermutation(int n, int k) { 4         int fac[10]; 5         bool vis[10]; 6         memset(vis, 0, sizeof(vis)); 7         fac[0] = 1; 8         for(int i=1;i<10;i++) 9             fac[i] = fac[i-1]*i;10         string res(n, 0);11         --k;12         for(int i=n-1;i>=0;i--){13             int temp = k/fac[i];14             int j=1;15             for(;j<10;j++){16                 if(vis[j] == 0)17                     temp--;18                 if(temp<0)19                     break;20             }21             res[n-i-1] = 0+j;22             vis[j] = 1;23             k%=fac[i];24         }25         return res;26     }27 };

 

LeetCode--Permutation Sequence