My simple c++ solution


  • 0
    K
    class Solution {
     public:
      string getPermutation(int n, int k) {
          int total=1, choice=0;
          string res="";
          for(int i=1; i <=n; i++)total = total * i;
          if(k > total)return res;
          string pool="123456789";
          
          choice=--k;
          for(int i=0; i < n; i++){
              total = total / (n-i);
              int index= choice/total;
              res+=pool[index];
              pool.erase(pool.begin()+index);
              choice= choice - index * total;
          }
          
          return res;
      }
    

    };


Log in to reply
 

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