zoukankan      html  css  js  c++  java
  • 【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;
            }
    };
  • 相关阅读:
    远程执行文件包含
    MSSQL2008 部署及开启远程连接
    schtasks 命令使用
    提权
    一个简单的Hibernate工具类HibernateUtil
    StandardServiceRegistryBuilder
    hibernate配置文件
    Hibernate入门
    mysql5.7.14安装与配置
    路漫漫兮其修远长,吾将上下而求索
  • 原文地址:https://www.cnblogs.com/xbf9xbf/p/4452448.html
Copyright © 2011-2022 走看看