【leetcode】 Permutation Sequence
问题:
分析:
实现:
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的更多相关文章
- 【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 ...
- 【Leetcode】Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 【LeetCode】Permutation全排列
1. Next Permutation 实现C++的std::next_permutation函数,重新排列范围内的元素,返回按照 字典序 排列的下一个值较大的组合.若其已经是最大排列,则返回最小排列 ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】回溯法 backtracking(共39题)
[10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses ( ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
随机推荐
- AC自动机(病毒侵袭 )
题目链接:https://cn.vjudge.net/contest/280743#problem/B 题目大意:中文题目 具体思路:AC自动机模板题,编号的时候注意,是按照给定的id进行编号的.然后 ...
- 日常训练赛 Problem C – Complete Naebbirac’s sequence
比赛链接https://vjudge.net/contest/256988#status/17111202012/C/0/ 大意:三个操作,使得输入的数中,从1-n,每一个数出现的次数相同. wa代码 ...
- Virut.ce-感染型病毒分析报告
1.样本概况 病毒名称 Virus.Win32.Virut.ce MD5 6A500B42FC27CC5546079138370C492F 文件大小 131 KB (134,144 字节) 壳信息 无 ...
- linux下的usb转串口的使用(修改)【转】
环境:Ubuntu 10.10 Server minicom是linux下串口通信的软件,它的使用完全依靠键盘的操作,虽然没有“超级终端”那么易用,但是使用习惯之后读者将会体会到它的高效与便利,下面将 ...
- 《JavaScript 高级程序设计》第一章:简介
JavaScript 历史 JavaScript的诞生的主要是当时的 netspace 公司谋求为自己的浏览器 Navigator 添加一种脚本语言,以便在本地客户端进行一些行为操作,而这一功能的需求 ...
- 如何简单区分Web前后端与MVC
MVC是开发所有软件所必须涉及的基本几个划分 M主要负责数据与模型,V主要负责显示C主要负责交互与业务所以不管是前端还是后端,都是有MVC的.MVC是一个对于软件简单的抽象,不管是M还是V,还是C都是 ...
- 【AtCoder】ARC098题解
C - Attention 枚举,计算前缀和即可 代码 #include <bits/stdc++.h> #define fi first #define se second #defin ...
- L3-004 肿瘤诊断 dfs bfs
在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环.给定病灶扫描切片中标注出的疑似肿瘤区域,请你计算肿瘤的体积. 输入格式: 输入第一行给出4个正整数:M.N.L.T,其中M和N是每张切片的尺寸(即每张切片 ...
- 011.KVM-V2V迁移
一 虚拟化存储池 1.1 创建虚拟化存储池 [root@kvm-host ~]# mkdir -p /data/vmfs 1.2 定义存储池与目录 [root@kvm-host ~]# virsh p ...
- 001.NTP简介
一 NTP简介 ntp服务器顾名思义就是时间同步服务器(Network Time Protocol),时间同步对于计划备份.入侵检测记录.分布式任务调度或者事务订单管理来说都是非常有必要的日常任务. ...