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;
}
};

最新文章

  1. 关于TxQBService报的错,腾讯你真牛B啊
  2. Eclipse启动时出现错误 An internal error occurred during: “Updating indexes”
  3. [开发笔记]-使用jquery获取url及url参数的方法
  4. 黄聪:wordpress伪静态的原理
  5. poj 2723
  6. java 凯撒大帝密码
  7. 【转】五种开源协议的比较(BSD, Apache, GPL, LGPL, MIT)
  8. 【转】OpenGL基础图形编程(一)
  9. mysql的replication(主从同步)总结
  10. SpringMVC中用@ParamVariable传递的参数包含斜杠(/)时,匹配不了报404错误的解决方案
  11. ASP.NET MVC5 学习笔记-5 测试
  12. 关于JAVA Project.waitfor()返回值是1
  13. 【前端】ACE Editor 简易使用示例
  14. 使用python和pygame绘制繁花曲线
  15. vue-----样式绑定 事件处理
  16. CodeForces-1132C-Painting the Fence-(前缀和)
  17. ubuntu16.04下无线网卡无法正常连网
  18. [WiX]Component Rules 101
  19. Window环境配置Mongodb
  20. LaTeX源代码显示宏包listings应用备忘之新语言定义

热门文章

  1. 读UNDO引发的db file sequential read
  2. POJ 2135 Farm Tour
  3. 【转】VS2013中如何解决error C4996: &#39;fopen&#39;问题
  4. linux串口驱动分析——打开设备
  5. JavaScript……
  6. Android手势操作
  7. 《STL源代码剖析》---stl_alloc.h阅读笔记
  8. mysql常用操作 mysql备份与恢复
  9. mybati之day02
  10. IFeatureClass.Search中的 Recycling 参数 - 浅谈.