LeetCode_Permutation Sequence
2024-10-10 06:08:59
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3): "123"
"132"
"213"
"231"
"312"
"321" Given n and k, return the kth permutation sequence. Note: Given n will be between 1 and 9 inclusive.
分析:数学的思路来做。
class Solution {
public:
string getPermutation(int n, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int A[] = {,, , , , , , , };
string res = "";
vector<bool> flag(n+, false);
for(int i = n-; i >= ; --i)
{
int pos = k / A[i];
if(k%A[i] == && pos > ) --pos;
k = k - pos * A[i];
for(int j = ; j <= n; ++j)
{
if(flag[j] == false){
if(pos == )
{
char c = '' + j;
res += c;
flag[j] = true;
break;
}else{
--pos;
}
}
}
} return res;
}
};
最新文章
- 关于TxQBService报的错,腾讯你真牛B啊
- Eclipse启动时出现错误 An internal error occurred during: “Updating indexes”
- [开发笔记]-使用jquery获取url及url参数的方法
- 黄聪:wordpress伪静态的原理
- poj 2723
- java 凯撒大帝密码
- 【转】五种开源协议的比较(BSD, Apache, GPL, LGPL, MIT)
- 【转】OpenGL基础图形编程(一)
- mysql的replication(主从同步)总结
- SpringMVC中用@ParamVariable传递的参数包含斜杠(/)时,匹配不了报404错误的解决方案
- ASP.NET MVC5 学习笔记-5 测试
- 关于JAVA Project.waitfor()返回值是1
- 【前端】ACE Editor 简易使用示例
- 使用python和pygame绘制繁花曲线
- vue-----样式绑定 事件处理
- CodeForces-1132C-Painting the Fence-(前缀和)
- ubuntu16.04下无线网卡无法正常连网
- [WiX]Component Rules 101
- Window环境配置Mongodb
- LaTeX源代码显示宏包listings应用备忘之新语言定义