• 【Permutation Sequence】cpp


    题目

    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)
        {
            std::stringstream result;
            // if the digit has been used
            int flag[10];
            for(int i = 1; i<10; ++i) flag[i]=1;
            // calculate each digit
            int factorical,quotient,remaind=k-1;
            for (int i = 1; i<=n; ++i)
            {
                 factorical = Solution::getFactorical(n-i);
                quotient = remaind/factorical;
                int tmp = 0;
                for(int j=1; j<=9; ++j){
                    tmp = tmp + flag[j];
                    if ( tmp==(quotient+1) ){
                        quotient = j;
                        break;
                    }
                }
                result << quotient;
                flag[quotient] = 0;
                remaind = remaind%factorical; // update remaind
            }
            return result.str();
        }
        static int getFactorical(int n){
            int result = 1;
            for (int i = 1; i <= n; ++i){
                result = result * i;
            }
            return result;
        }
    };

    Tips:

    1. 主要思路是康托编码,具体什么是康托编码(http://blog.sina.com.cn/s/blog_4bf7b6580100l2zs.html

    2. 把计算阶乘的代码单独列出来

    3. 对于c++如何将int转为字符不太懂,因此用了sstream的这个方法,后面遇到其他的方法再改进一下。

    ======================================================

    第二次过这道题,对康托编码的算法有些生疏了,参考了blog才回忆起来。然后扫了一些细节,形成了自己的代码。

    class Solution {
    public:
            string getPermutation(int n, int k)
            {
                vector<char> ret;
                vector<bool> visit(9,false);
                k = k-1;
                for ( int i=n-1; i>=0; --i)
                {
                    int factor = Solution::factorial(i);
                    int num_less = k / factor;
                    // find num less
                    int j=0;
                    int count = 0;
                    for ( ; j<9; ++j )
                    {
                        if ( !visit[j] )
                        {
                            if ( count==num_less ) break;
                            count++;
                        }
                    }
                    visit[j] = true;
                    ret.push_back(j+'1');
                    k = k % factor; 
                }
                return string(ret.begin(),ret.end());
            }
            static int factorial(int n)
            {
                if ( n==0 || n==1 ) return 1;
                int ret = 1;
                for ( int i = 1; i<=n; ++i) ret = ret * i;
                return ret;
            }
    };
  • 相关阅读:
    HTML图片轮播
    HTML横向二级导航
    HTML 5的革新——语义化标签
    Git撤销修改
    git add -A 和 git add . 的区别
    git reset HEAD 与 git reset --hard HEAD的区别
    Git 工作区、暂存区和版本库
    Python笔记 #21# DHNN
    算法导论(第三版)练习 6.1-1 ~ 6.1-7
    算法导论(第三版)练习 4.1-1 ~ 4.1-5
  • 原文地址:https://www.cnblogs.com/xbf9xbf/p/4452448.html
Copyright © 2020-2023  润新知