leetcode 60. Permutation Sequence(康托展开)
描述:
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.
思路:
参见我的博文:全排列的编码与解码:康托展开
代码:
class Solution {
public:
string getPermutation(int n, int k) {
vector<int> temp;
vector<int> fac;
vector<int> nums; int next_k,now_seq;
string result; if( n== ){
result="";
return result;
} //generate nums
for( int i=;i<n;i++ )
nums.push_back(i+);
//calculate factorial times,fac[0]=(n-1)!,fac[1]=(n-2)!
for( int i=n-;i>=;i-- ){
if( i==n- )
fac.push_back();
else
fac.insert(fac.begin(),(n-i-)*(*fac.begin()));
}
//calculate every place from high
next_k=k;
for( int i=;i<n-;i++ ){
now_seq=(next_k-)/fac[i]+;
temp.push_back(nums[now_seq-]);
nums.erase(nums.begin()+now_seq-);
next_k=(next_k-)%fac[i]+;
}
temp.push_back(nums[]);
//into string
for( int i=;i<n;i++ ){
result.append(,''+temp[i]);
}
return result;
}
};
leetcode 60. Permutation Sequence(康托展开)的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- [LeetCode] 60. Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- leetCode 60.Permutation Sequence (排列序列) 解题思路和方法
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- LeetCode: 60. Permutation Sequence(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- [LeetCode]60. Permutation Sequence求全排列第k个
/* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- 【一天一道LeetCode】#60. Permutation Sequence.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- BZOJ-1923-外星千足虫-SDOI2010
描写叙述 分析 首先看上去这貌似是一个高斯消元的题目, 直觉吧- 每次给出的就相当于是一个方程. 然后非常easy想到n条虫子n个x, x_i的系数为0表示这个方程中没有i, 否则为1. 然后系数乘以 ...
- Codeforces 191C Fools and Roads(树链拆分)
题目链接:Codeforces 191C Fools and Roads 题目大意:给定一个N节点的数.然后有M次操作,每次从u移动到v.问说每条边被移动过的次数. 解题思路:树链剖分维护边,用一个数 ...
- ASP.NET MVC 学习之路-4
本文在于巩固基础 模型绑定 从URL 获取值 public ActionResult About(int id) { ViewBag.Id = id; return View(); } @{ View ...
- collectionView关于点击事件
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)i ...
- C#核编之System.Environment类
在前面的例子中用来了Environment.GetCommandLineArgs()这个方法,这个方法就是获取用户的命令行输入,是Environment类的方法之一,该方法的返回值是string[] ...
- Java异常分类 转载
Java异常分类 http://blog.csdn.net/woshixuye/article/details/8230407 一.基本概念 看java的异常结构图 Throwable是所有异 ...
- [剖析Javascript原理]1.原生数据类型
一.原生数据类型 JS共有5种原生数据类型: Boolean true或者false String 字符串,在单引号或者双引号之间(不存在字符类型) Number 整数或者浮点数 Null 空 und ...
- HTML中的uniqueID
Web页面上元素的name属性本身是可以重复的,理论上讲id是不可以重复的,但是现在的浏览器对重复的id都是默许的,可能有时候页面是需要一个唯一编号的.IE浏览器为页面上的所有元素都是提供了一个唯一名 ...
- javascript每日一练—运动
1.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...
- Cortex-M3 动态加载一(地址无关代码实现)
这篇文章是自己疑惑究竟地址无关性是如何实现,然后查看汇编和CPU指令手册,最后分析解除自己疑惑的,高手不要鄙视,哈哈. 编译C代码时候需要制定--acps/ropi选项,如下例子: void Syst ...