标签:
class Solution { public: vector<int> buf; string getPermutation(int n, int k) { string result; int index = 0; for(index =1; index <=n; index++) { buf.push_back(index); } GetNextPermutation(0,n,k); result.resize(n); for(index =0; index <n ; index++) { result[index] = buf[index] + ‘0‘; } return result; } bool GetNextPermutation(int index,int n,int k) { int num =0; int i=0; num = GetMultiResult(n-index - 1); if(index == n) return true; for(i = 0; i< n - index; i++) { if(num*(i+1) >= k) { AdjustOrder(index,index + i); return GetNextPermutation(index + 1,n,k-(num*i)); } } return false; } void AdjustOrder(int startindex,int index ) { int i=0,temp = buf[index]; for(i=index - 1; i>= startindex; i--) { buf[i+1] = buf[i]; } buf[startindex] = temp; } int GetMultiResult(int n) { int result = 1; while(n>1) { result = result* n; n = n-1; } return result; } };
标签:
原文地址:http://www.cnblogs.com/xgcode/p/4322125.html