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

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

Given n and k, return the kth permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

思路:有一种方法,我们可以递归求解出所有的排列,然后取出第k个,但是这么操作,既复杂,也没有必要。

我们来一起找找数学规律,n个数的排列总共有n!个组合,基于这个性质我们可以得到某一位对应的数字是哪一个。如,当前长度是n,我们知道每个相同起始元素都对应(n-1)!个排列,也就是每隔(n-1)!个排列就会按照大小顺序更换一个起始元素。因此,我们只要对当前的k对(n-1)!取余,得到的数字就是当前剩余数组的index, k /(n-1)!就是当前元素。如此递归下去,知道数组中没有元素。我们需要维护一个数组记录当前的元素,每次得到一个元素加入结果数组,就要把它从剩余数组中移除,剩余数组重新排序,接下来依然按照index取元素。

假设有n个元素,第K个permutation是

if(round > 0)
                factorial /= round;

a1, a2, a3, .....   ..., an

那么a1是哪一个数字呢?

那么这里,我们把a1去掉,那么剩下的permutation为

a2, a3, .... .... an, 共计n-1个元素。 n-1个元素共有(n-1)!组排列,那么这里就可以知道

设变量K1 = K

a1 = K1 / (n-1)!

同理,a2的值可以推导为

a2 = K2 / (n-2)!

K2 = K1 % (n-1)!

.......

a(n-1) = K(n-1) / 1!

K(n-1) = K(n-2) /2!

an = K(n-1)

Attention:

1. k--; 让下标从0开始,这样就统一了下标和数组下标索引。

2. 每次得到的元素,转成字符后,添加到结果字符串中。用加‘0’的方式

ret += ('0' + nums[index]);        //转成字符添加进字符串

3. vector的erase函数 删除后,会改变容器大小和排序。

iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);

参数必须是迭代器。

 nums.erase(nums.begin() + index);  //参数必须是迭代器

4. 再递减 (n-1)!时,需要判断分母是否大于0.

if(round > 0)
   factorial /= round;

5. 我们需要判断到round = 0, 从 (n-1)! 到 0!, 总共添加n回,才能得到结果字符串

 while(round >= 0)

复杂度:O(N), 进行N各回合。

AC Code:

class Solution {
public:
    string getPermutation(int n, int k) {
        if(n <= 0) return "";
        //转换成从0开始
        k--;

        string ret;
        int factorial = 1;
        //计算(n-1)!
        for(int i = 2; i < n; i++)
        {
            factorial *= i;
        }

        vector<int> nums;
        for(int i = 1; i <= n; i++)
        {
            nums.push_back(i);
        }

        int round = n - 1;
        while(round >= 0)
        {
            int index = k / factorial;
            k %= factorial;
            ret += ('0' + nums[index]);        //转成字符添加进字符串
            nums.erase(nums.begin() + index);  //参数必须是迭代器
            if(round > 0)
                factorial /= round;
            round--;
        }

        return ret;
    }
};

时间: 2024-10-25 20:43:28

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

【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

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

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&