标签:io strong for sp on c r bs leetcode
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.
Note: Given n will be between 1 and 9 inclusive.
Solution:
计算 1~n 数字的第k个排列
思路1:从小到大生成同时计数,直到第k个。 肯定超时试都不用试。。
思路2:直接根据规律计算第k个排列。
分析:1~n个数共有n!个排列,1开头的有(n-1)!个,2开头的(n-1)!个,...n开头的有(n-1)!个。因此用k/(n-1)!就确定了第一位数字,然后依次类推(用过的数字要去除),继续在(n-1)!个数中找第k%(n-1)!个数。
[leetcode] Permutation Sequence
标签:io strong for sp on c r bs leetcode
原文地址:http://www.cnblogs.com/Phoebe815/p/3997590.html