c++ solution


  • 0
    B
    class Solution {
    public:
        string getPermutation(int n, int k) {
            vector<string> num ;
            k-=1;
            string result="";
            for(int i=0;i<n;i++) num.push_back(to_string(i+1));
            
            while(num.size()){
                int fac=1;
                for(int i=2;i<num.size();i++) fac*=i;
                
                result+=num[k/fac];
                num.erase(num.begin()+k/fac);
                k%=fac;
            }
            
            return result;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.