问题:

对于给定序列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. python3之SQLAlchemy

    1.SQLAlchemy介绍 SQLAlchemy是Python SQL工具包和对象关系映射器,为应用程序开发人员提供了SQL的全部功能和灵活性. 它提供了一整套众所周知的企业级持久性模式,专为高效和 ...

  2. Binary Representation

    Given a (decimal - e.g. 3.72) number that is passed in as a string, return the binary representation ...

  3. Mysql读写分离-Amoeba Proxy

    参考:http://www.linuxidc.com/Linux/2015-10/124115.htm 一个完整的MySQL读写分离环境包括以下几个部分: 应用程序client database pr ...

  4. MongoDB:数据导入CSV文件之错误记录

    测试主机1:Windows 10,MongoDB 3.6.3,WPS 10.1,Notepad++ 7.5.3, 测试主机2:Ubuntu 16.04,MongoDB 4, 今天测试了将数据从文件—— ...

  5. Java中final关键字概述

    使用final修饰过的变量都不可以改变: 1.final修饰变量 恒定不变的属性,可以用final关键字来修饰: 变量名建议全部使用大写 final修饰的变量不能改变,如果程序中重新赋值,编译报错 例 ...

  6. express中间件代理实现跨域

    前端代码 var xhr = new XMLHttpRequest(); xhr.open('post', 'http://localhost:3000', true); xhr.onreadysta ...

  7. 阿里云服务器配置nginx和PHP

    1. 安装及启动nginx 输入yum install nginx命令进行nginx的安装,当需要确认时输入”y“确认. yum install nginx 安装完成后,输入service nginx ...

  8. mkfs

    mkfs 命令  linux格式化磁盘命令 指令:mkfs 使用权限 : 超级使用者 使用方式 : mkfs [-V] [-t fstype] [fs-options] filesys [blocks ...

  9. Oracle学习笔记:with as子查询用法

    With as短语,也叫做子查询部分(subquery factoring),可以定义一个SQL片断,该SQL片断会被整个SQL语句用到.该语句会在真正的查询之前预先构造一个临时表,之后可以多次使用做 ...

  10. Java编程的逻辑 (67) - 线程的基本协作机制 (上)

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...