*【python/M/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”
Given n and k, return the kth permutation sequence.

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

基本思路

还是不懂,我还不懂,排列问题还是没搞懂
n 个数字有 n!种全排列,每种数字开头的全排列有 (n - 1)!(n!/ n)种。
所以用 k / (n - 1)! 就可以得到第 k 个全排列是以第几个数字开头的。
用 k % (n - 1)! 就可以得到第 k 个全排列是某个数字开头的全排列中的第几个。
这又变成了最初的问题设置。
对于以某个数字开头的全排列(第一个数字固定后的全排列,不再理会第一个数字),它有 (n - 1)! 种全排列,那么这些全排列中,每个数字开头的全排列有 (n - 2)! ((n-1)! / (n-1))。
依次类推。

在代码中为了使每次选中的数字不会重复。先用一个数组记录下所有数字(1 到 n),每次使用了某个数字后,就把该数字从数组中移除。
上述我们提到的“第 k 个全排列是以第几个数字开头“,这”第几个数字“并不是指该数字本身,而是数组中的”第几个数字“。

代码

class Solution:
    def getPermutation(self, n, k):
        """
        :type n: int
        :type k: int
        :rtype: str
        """
        res = ''
        k -= 1
        fac = 1

        # 第一位确定时的全排列数量
        for i in range(1, n): 
            fac *= i

        num = [str(i) for i in range(1,n+1)]

        for i in reversed(range(n)):
            # 下标是k//fac的数字
            curr = num[k//fac]
            res += str(curr)
            # 使用过的数字移除
            num.remove(curr)
            if i !=0:
                k %= fac
                # 再固定一位时候的全排列数量
                fac = fac//i 
        return res

运行结果

40ms

#include <iostream> #include <algorithm> using namespace std; struct Matrix{ int m, n; int **val; Matrix(){} Matrix(int m_, int n_){ m = m_; n = n_; this->val = (int**)malloc(sizeof(int*)*m); for(int i=0;i<m;i++){ this->val[i] = (int*)malloc(sizeof(int)*n); } } void in(){ for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ scanf("%d", &this->val[i][j]); } } } void out(){ for(int i=0;i<m;i++){ printf("%d", this->val[i][0]); for(int j=1;j<n;j++){ printf(" %d", this->val[i][j]); } printf("\n"); } } int Determinant_1 (){ // 请在这里补充代码,完成本关任务 /********* Begin *********/ /********* End *********/ } int Determinant_2 (){ // 请在这里补充代码,完成本关任务 /********* Begin *********/ /********* End *********/ } int Determinant_3 (){ // 请在这里补充代码,完成本关任务 /********* Begin *********/ /********* End *********/ } int Inverse_Number(int n, int arr[]){ // 请在这里补充代码,完成本关任务 /********* Begin *********/ /********* End *********/ } int Determinant_n (){ // 请在这里补充代码,完成本关任务 /********* Begin *********/ /********* End *********/ } int Determinant (){ if(this->n==1){ return Determinant_1(); }else if(this->n==2){ return Determinant_2(); }else if(this->n==3){ return Determinant_3(); }else { return Determinant_n(); } } }; int main(int argc, const char * argv[]) { int n; scanf("%d", &n); Matrix A(n,n); A.in(); int det = A.Determinant(); printf("Det(A)=%d\n", det); return 0;}将这串代码补全
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值