问题:

对于给定序列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. 关于caffe的安装问题

    在caffe的安装过程中,出现 /usr/bin/ld: cannot find -lcblas /usr/bin/ld: cannot find -latlas的问题 这时解决方案为http://s ...

  2. 20155303 实验四 Android程序设计

    20155303 实验四 Android程序设计 目录 第24章:初识Android 任务一: 完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号 学习 ...

  3. linux批量关闭进程

    ps aux | grep gunicorn_api | awk '{print $2}' | xargs kill -9 gunicorn 换成你的关键字即可.

  4. python基础-pthon

    1)python 由Guido开发 2)编译(compile)型:通过编译器把代码直接生成一个可执行文件. 比如把英语书一次性翻译成中文书.语言有:c,C++等 解释型:边编译边执行.语言如:java ...

  5. 浅谈js设计模式 — 享元模式

    享元(flyweight)模式是一种用于性能优化的模式,“fly”在这里是苍蝇的意思,意为蝇量级.享元模式的核心是运用共享技术来有效支持大量细粒度的对象. 假设有个内衣工厂,目前的产品有 50种男式内 ...

  6. ***linux下安装xampp,XAMPP目录结构(阿里云安装xampp)

    XAMPP目录结构 重要的文件和目录 文件/目录                              用途 /opt/lampp/bin/ XAMPP 命令库.例如 /opt/lampp/bin ...

  7. vSphere Web Client 6.5 如何上传ISO文件

    vSphere Web Client 6.5 如何上传ISO文件? 1,先开启SSH功能. WEB登陆管理端,选中一台主机,配置-安全配置文件-服务编辑-SSH项-起动. 2,用SFTP上传ISO文件 ...

  8. json多态序列化

    https://blog.csdn.net/java_huashan/article/details/46428971 https://blog.csdn.net/bruce128/article/d ...

  9. maven正确的集成命令-U -B 等

    在持续集成服务器上使用怎样的 mvn 命令集成项目,这个问题乍一看答案很显然,不就是 mvn clean install 么?事实上比较好的集成命令会稍微复杂些,下面是一些总结: 不要忘了clean: ...

  10. 【LOJ】#2523. 「HAOI2018」奇怪的背包

    题解 复杂度怎么算也要2s的题怎么0.5s就跑完了,迷啊 这个题简直算完复杂度不敢写,写了就赚飞了好吧 根据裴蜀定理,显然选出的数和P的gcd是w的约数 我们考虑枚举\(P\)的约数,上限当然是\(\ ...