leetcode 之 Permutation Sequence
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.
如果有n个元素,第K个permutation是a1, a2, a3, ..... ..., an,那么a1是哪一个数字呢?
那么这里,我们把a1去掉,那么剩下的permutation为:a2, a3, .... .... an, 共计n-1个元素。 n-1个元素共同拥有(n-1)!组排列,那么这里就能够知道
设变量K1 = K-1
a1 = K1 / (n-1)!// 即a1是1~n中未使用过的第a1个元素,比如,刚開始时,若a1 = 1,则结果的第一个元素是2
同理,a2的值能够推导为
K2 = K1 % (n-1)! //前面的a1*(n-1)!已经增加,所以要去掉
a2 = K2 / (n-2)!
。。。。。
K(n-1) = K(n-2) /2!
a(n-1) = K(n-1) / 1!
an = K(n-1)
class Solution {
public:
string getPermutation(int n, int k)
{
int data[10];//保存阶层的值
bool hashUse[10];
memset(hashUse,false,sizeof(bool)*10);
int i,j;
data[0] = data[1] = 1;
for(i = 2;i <= n;++i)data[i] = data[i-1] * i;
k --;
string res;
for(i = n - 1;i >= 0;--i)
{
int value = k / data[i];
for(j = 1;j <= n;++j)//查找第value大且未使用过的值
{
if(!hashUse[j])value--;
if(value < 0)break;
}
hashUse[j] = true;
res += j + '0';
k %= data[i];
}
return res;
}
};
leetcode 之 Permutation Sequence的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- Java for LeetCode 060 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】 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 60. Permutation Sequence(康托展开)
描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- 【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 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】 Permutation Sequence
问题: 对于给定序列1...n,permutations共同拥有 n!个,那么随意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小開始直接到k, ...
随机推荐
- golang各版本的变化
https://golang.org/doc/https://golang.org/doc/go1.6https://golang.org/doc/go1.5https://golang.org/do ...
- cookie的path和domain參数实例解析
一句话概括两个參数含义各为: path表示cookie所在的文件夹 domain表示的是cookie所在的域,默觉得请求的地址 首先改动我们的 hosts 文件 我本机内网ip 192.168.1.1 ...
- Effective C++_笔记_条款00_基本术语
(整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 下面是每一位C++程序员都应该了解的C++词汇. 1 C++中 ...
- JS - 6款鼠标悬停效果
下载地址:http://www.lanrentuku.com/js/tupian-1093.html
- JAVA之成员变量初始化
java中成员变量的初始化,如果是普通成员变量可以赋初值,如果没赋初值,则系统会自动赋初值.而对于局部变量必须手动赋初值. package MyText2; public class MyText1 ...
- 转载【浅谈ThreadPool 线程池】
浅谈ThreadPool 线程池 http://www.cnblogs.com/xugang/archive/2010/04/20/1716042.html
- 【Demo 0015】位置服务及地图
本章学习要点: 1. 掌握位置相关类(CLLocationManager,MKUserLocation) 基本用法; 2. 掌握地图视图(MKMapView)基本用法; ...
- Velocity教程-脚本语法详解(转)
Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象. 当Veloci ...
- PHP从数据库获取的下拉树
<?php include "config.php"; include "mysql.php"; $db = new Mysql('test'); //几 ...
- CentOS5.6下安装Oracle10G软件 【保留报错经验】
CentOS5.6下安装Oracle10G ****************************************************************************** ...