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

  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.

class Solution {
public:
    string getPermutation(int n, int k) {
        vector<char> pers(n, '1');
        int fact = n;
        for (int i=1; i<n; i++) {
            fact *= i;
            pers[i] = '1' + i;
        }

        k--;
        string ans;
        for (int i=0; i<n; i++) {
            fact /= (n-i);
            const int j = k / fact;
            k %= fact;

            ans.push_back(pers[j]);
            pers.erase(pers.begin()+j);
        }

        return ans;
    }
};

在leetcode上,实际执行时间为5ms。

基本思路:

参照普通进制的方法,求出每位的权重,从高位到低位利用整除和取余求出每位的值。

区别为,此权重都是阶乘值,不像普通进制,为基数的各次幂。

时间: 2024-08-10 21:38:57

Permutation Sequence -- leetcode的相关文章

[LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence

一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全排列(Permutation Sequence):给出一个全排列,求其所在位置. 二.例题 1. 求下一个全排列,Next permuation Implement next permutation, which rearranges numbers into the lexicographically ne

LeetCode: Permutation Sequence 解题报告

Permutation Sequence https://oj.leetcode.com/problems/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&

【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: Permutation Sequence [059]

[题目] 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——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]Permutation Sequence @ Python

原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: 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&

【一天一道LeetCode】#60. Permutation Sequence.

一天一道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): 1:"123" 2:"132" ? 3 : "213" 4 :&quo

[leetcode]Permutation Sequence

问题描述: The set [1,2,3,-,n] contains a total ofn! 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

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