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

  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!之间的关系,第k个序列应该在哪些位置提升几个数字。

代码:

 //consider if k > n! ?
    string getPermutation(int n, int k) {   //C++
        //the first
        vector<char> base(n,'0');
        for(int i = 1; i<=n; i++)
            base[i-1] = i +'0';

        vector<int> multi(n);
        int temp = 1;
        multi[0] = 1;
        for(int i =1; i < n; i++){
            temp *= i;
            multi[i] = temp;
        }

        k--;
        for(int i = 0; i< n-1; i++){
            int temp = k/multi[n-i-1];

            int pos = i+temp;
            char c = base[pos];

            for(int j =pos-1 ; j>=i; j-- )
                base[j+1] = base[j];

            base[i] = c;
            k = k%multi[n-i-1];
        }

        //to String
        string result = "";
        for(int i = 0; i < n; i++)
            result += (base[i]);
        return result;
    }
时间: 2024-10-11 01:53:53

[leetcode]Permutation Sequence的相关文章

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

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] “全排列”问题系列(二) - 基于全排列本身的问题,例题: 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

问题: 对于给定序列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.

一天一道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

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