MY C++ Solution 28ms


  • 1
    N
    class Solution {
      public:
    string intToRoman(int num) {
        string roman;
        vector<string>result;
             while(num != 0){
          if(num >= 1000){
           result.push_back("M");
            num -= 1000;
            continue;
          }
          if(num <1000 && num >=900){
           result.push_back("CM");
             num -= 900;
             continue;
          }
             if(num <900 && num >=500){
             result.push_back("D");
             num -= 500;
             continue;
          }
             if(num <500 && num >=400){
             result.push_back("CD");
             num -= 400; 
             continue;
          }
             if(num <400 && num >=100){
             result.push_back("C");
             num -= 100; 
             continue;
             }
             if(num <100 && num >= 90){
              result.push_back("XC");
             num -= 90; 
             continue;
             }
             if(num <90 && num >= 50){
              result.push_back("L");
             num -= 50; 
             continue;
             }
             if(num <50 && num >= 40){
              result.push_back("XL");
             num -= 40; 
             continue;
             }
             if(num <40 && num >= 10){
              result.push_back("X");
             num -= 10; 
             continue;
             }
              if(num == 9){
              result.push_back("IX");
             num -= 9; 
             continue;
             }
             if(num <9 && num >= 5){
              result.push_back("V");
             num -= 5; 
             continue;
             }
             
             if(num <5 && num>=4){
              result.push_back("IV");
             num -= 4;
             continue;
             }
             if(num <4 && num >= 1){
              result.push_back("I");
             num -= 1; 
             continue;
             }
      }
      for(int i = 0; i < result.size(); ++i)
      {  roman+=result[i];
      }
      return roman;}}

Log in to reply
 

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