问题:

对于给定序列1...n,permutations共同拥有 n!个,那么随意给定k,返回第k个permutation。0 < n < 10。

分析:

这个问题要是从最小開始直接到k,预计会超时,受10进制转换为二进制的启示,对于排列,比方 1,2,3 是第一个,那么3!= 6,所以第6个就是3,2,1。也就是说,从開始的最小的序列開始,到最大的序列,就是序列个数的阶乘数。那么在1,3 , 2的时候呢?调整一下,变成2,1,3,就能够继续。

实现:

int getFactorial(int n)
{
int factorial[10] = {-1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
return factorial[n];
}
int checkFactorial(int n){
// n is small, use liner search.
int factorial[10] = {-1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
for (int i = 1; i < 10; ++i)
{
if(n >= factorial[i] && n < factorial[i + 1])
return i;
}
return 0;
}
void nextPermutetion(string &num)
{
int i = num.size() - 1;
while (i >= 1)
{
if(num[i] > num[i - 1])
{
--i;
int ii = num.size() - 1;
while (ii > i && num[ii] <= num[i]) --ii;
if(ii > i)
{
swap(num[i], num[ii]);
reverse(num.begin() + i + 1, num.end());
break;
}
}
else
--i;
}
}
string getPermutation(int n, int k) { if(k > getFactorial(n))
return "";
if(k <= 0 || 0 > n || n >= 10)
return "";
//init the permutation.
string permu(n,'0');
for(int i = 0; i < n; ++i)
permu[i] = i + 1 + '0'; if(k == 1)
return permu;
while (k > 0)
{
int fac = checkFactorial(k);
//adjust
if(permu[n - 1] <= permu[n - fac])
{
nextPermutetion(permu);
} reverse(permu.begin() + (n - fac), permu.end());
k -= getFactorial(fac);
}
return permu;
}

说明:实现有些复杂,http://blog.csdn.net/doc_sgl/article/details/12840715 这里有个简单的纯数学解法。

【leetcode】 Permutation Sequence的更多相关文章

  1. 【leetcode】 Permutation Sequence (middle)

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  2. 【Leetcode】Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  3. 【LeetCode】Permutation全排列

    1. Next Permutation 实现C++的std::next_permutation函数,重新排列范围内的元素,返回按照 字典序 排列的下一个值较大的组合.若其已经是最大排列,则返回最小排列 ...

  4. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  5. 【LeetCode】回溯法 backtracking(共39题)

    [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses ( ...

  6. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  7. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  8. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  9. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

随机推荐

  1. 解决Tomcat6解压版在64位windows系统上无法启动服务的问题

    解决Tomcat6解压版在64位windows系统上无法启动服务的问题         由于客户环境为64位windows系统,开发环境一直用32位.tomcat使用6.0.20非安装版.部署时发现在 ...

  2. 在一台win10上启动多个mysql

    1.因为项目需要用一个已经有数据的mysql,而我之前已经安装了一个mysql(之前的mysql上面也是有东西,不想删除)  想办法.... mysqld.exe --defaults-file=D: ...

  3. XSS小游戏通关Writeup

    源码下载:https://files.cnblogs.com/files/nul1/xss%E7%BB%83%E4%B9%A0%E5%B0%8F%E6%B8%B8%E6%88%8F.zip 我也没啥可 ...

  4. 【源码阅读】VS调试mimikatz-改造法国神器mimikatz执行就获取明文密码

    0x1 概要 记得某位同学提起在XXX得到了一个一键生成明文的工具,觉得很是神奇... 然而我一看图标就知道是mimikatz,这工具是开源的,只要改两行代码就可以实现写死命令了. 顺带讲讲编译过程中 ...

  5. mysql innobackupex 备份及恢复

    ----------------------------------全量备份恢复-------------------------------------1.生成一个完整的备份 innobackupe ...

  6. Gitlab & Github

    windwos上Git的使用 软件下载地址:https://github.com/git-for-windows/git/releases/download/v2.15.1.windows.2/Git ...

  7. Notes for building gimp-print

    First try, build gimp-print on ubuntu. 1. Install all dependencies. sudo apt-get install libcups2-de ...

  8. ngResource和REST介绍

    ngResource和REST介绍 一.RESTful介绍 RESTful维基百科 REST(表征性状态传输,Representational State Transfer)是Roy Fielding ...

  9. ld: warning: directory not found for option '-F/Users/Jason/Project/xxx'

    解决方法: 选择项目名称----->Targets----->Build Settings----->Search Paths----->Library Search Path ...

  10. PLSQL Developer连接远程oracle配置

    在windows机器上不想安装oracle或者oracle的客户端,我们怎么使用PLSQL Developer工具呢?答案如下: 环境                       windows7   ...