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):

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "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;
    }
};
时间: 2024-11-05 02:21:22

leetcode 之 Permutation Sequence的相关文章

【leetcode】 Permutation Sequence

问题: 对于给定序列1...n,permutations共有 n!个,那么任意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小开始直接到k,估计会超时,受10进制转换为二进制的启发,对于排列,比如 1,2,3 是第一个,那么3!= 6,所以第6个就是3,2,1.也就是说,从开始的最小的序列开始,到最大的序列,就是序列个数的阶乘数.那么在1,3 , 2的时候呢?调整一下,变成2,1,3,就可以继续. 实现: int getFactorial(int n

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): Given n and k, return the kth permutation sequence. Note: Given n will be between

【leetcode】 Permutation Sequence (middle)

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" "3

[C++]LeetCode: 114 Permutation Sequence(返回第k个阶乘序列——寻找数学规律)

题目: 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" &q

【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" &q

LeetCode 59 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" "

leetcode 60 Permutation Sequence ----- java

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" "3

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" "

19.2.9 [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 for n = 3: "123" "132" "213" "231" "312" "321&