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 ...
随机推荐
- servlet上传文件报错(一)
1.详细报错例如以下: org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multip ...
- 【双向广搜+逆序数优化】【HDU1043】【八数码】
HDU上的八数码 数据强的一B 首先:双向广搜 先处理正向搜索,再处理反向搜索,直至中途相遇 visit 和 队列都是独立的. 可以用一个过程来完成这2个操作,减少代码量.(一般还要个深度数组) 优化 ...
- Mschart绘制图表之X轴为时间的设置方式
最近使用C#开发图表,比较了DirectorChart,DontNetCharting,TeeChart,最终选用微软的mschart开发,对于X轴作为时间轴探索了好久,终于实现了想要的效果. 界面效 ...
- iOS进阶:Objective-C runtime(一)
第一次看到runtime时,觉得太高大上,动态获取方法.属性等简直厉害的不要不要的.在经过查找资料+实践后,发现runtime并没有想象中那么复杂,接下来对runtime进行基本的介绍. 要使用运行时 ...
- 漫谈AOP开发之初探AOP及AspectJ的用法
一.为什么需要AOP技术 AOP 是一个很成熟的技术. 假如项目中有方法A.方法B.方法C……等多个方法, 如果项目需要为方法A.方法B.方法C……这批方法增加具有通用性质的横切处理. 下图可以形 ...
- leetcode Divide Two Integers python
class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...
- [整理]DLL延时加载 && 设置进程私有环境变量
DLL延时加载鉴于静态和动态加载之间,即无需在代码中显示加载但它内队依然是动态加载的方式只是系统帮处理了.这样做好处是: 1. 可以加快启动时间(因为它是动态加载在需要的时间加载), 2. 减小编写L ...
- 从零开始PHP学习 - 第四天
写这个系列文章主要是为了督促自己 每天定时 定量消化一些知识! 同时也为了让需要的人 学到点啥~! 本人技术实在不高!本文中可能会有错误!希望大家发现后能提醒一下我和大家! 偷偷说下 本教程最后的目 ...
- ParseChat应用源码ios版
ParseChat是一个完全原生的iPhone应用程序,用于创建实时的.基于文本的Parse聊天室.功能:支持多台设备之间的实时聊天,可动态添加新的聊天室,支持基本配置,可发送和接收音效以及任意大小的 ...
- 2 kNN-K-Nearest Neighbors algorithm k邻近算法(二)
2.3 示例:手写识别系统 2.3 .1 准备数据:将图像转换为测试向量 训练样本:trainingDigits 2000个例子,每个数字大约200个样本 测试数据:testDigits 大约900个 ...