本文主要是介绍**LeetCode 60. Permutation Sequence,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
https://leetcode.com/problems/permutation-sequence/
康拓展开和逆康拓展开
自己推一推 参考http://www.cnblogs.com/hxsyl/archive/2012/04/11/2443009.html
class Solution {
public:string getPermutation(int n, int k) {k--;for(int i=0;i<=n;i++){vis[i]=0;if(i == 0)jie[i]=1;else jie[i] = (i)*jie[i-1];}string ret;for(int i=n-1;i>0;i--) {int div = k/jie[i];int mod = k%jie[i];ret = ret + getDiv(div+1,n);//cout << "div=" << div << " " << " mod=" << mod << endl;k = mod;}for(int i=1;i<=n;i++)if(!vis[i])ret = ret + (char)(i+'0');return ret;}char getDiv(int d, int n) {int cnt=0;for(int i=1;i<=n;i++) {if(!vis[i]) {cnt++;if( cnt == d ) {vis[i] = 1;return (char)(i+'0');}}}return '0';}
private:int vis[10], jie[10];
};
这篇关于**LeetCode 60. Permutation Sequence的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!