Permutation Sequence
来源:互联网 发布:电信80端口 编辑:程序博客网 时间:2024/06/02 06:26
-----QUESTION-----
The set [1,2,3,…,n]
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for
"123"
"132"
"213"
"231"
"312"
"321"
Given
Note:
-----SOLUTION-----
class Solution {public: string getPermutation(int n, int k) { string result = ""; bool hasUsed[n+1]; //whether the number has used in the string for(int i = 0; i<=n; i++) { hasUsed[i] = false; } int dp[n]; //permutation number of a digit with i width dp[0] = 1; dp[1] = 1; for(int i = 2; i< n; i++) { dp[i] = i* dp[i-1]; } int num; stringstream ss; string str; for(int i = n; i>0; i--) { num = k/dp[i-1]; if(k%dp[i-1] != 0) num+=1; int counter = 0; int j; for(j = 1; j<=n && counter!=num; j++) { if(!hasUsed[j])counter++; if(counter == num) break; } hasUsed[j] = true; ss.clear(); ss<<j; ss>>str; result = result +str; k = k-(num-1)*dp[i-1]; } return result; }};
0 0
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- android 进度加载条
- oracle查看分区的数据
- 14_C# 实现VMS客户端——控件_PanelEx
- VAO与VBO
- 首款Apple Watch智能手表游戏发布
- Permutation Sequence
- get,post ,put,delete 与查,改,增,删
- 动态规划之最长连续公共子序列
- UNIX时间戳和格式化日期
- 判断字符串是否为空,包括判断null/undefined
- TCP 的那些事儿(下)
- 谷歌收购健康技术公司Lift Labs发力电子医疗
- linux下安装和运行wireshark
- 恰年建站新风尚受到了各大门户网站的争相报道