leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

integer-to-roman.cpp (1167B)


      1 #include <iostream>
      2 #include <map>
      3 using namespace std;
      4 
      5 //Leet Ratings
      6 //       Speed  Memory
      7 //Total  32ms   12.9MB    
      8 //Beats  5.2%   7.2%
      9 //
     10 
     11 
     12 string intToRoman(int num) {
     13     //Create hashmap
     14     map <int ,string> symbols;
     15     symbols.insert({1, "I"});
     16     symbols.insert({4, "IV"});
     17     symbols.insert({5, "V"});
     18     symbols.insert({9, "IX"});
     19     symbols.insert({10, "X"});
     20     symbols.insert({40, "XL"});
     21     symbols.insert({50, "L"});
     22     symbols.insert({90, "XC"});
     23     symbols.insert({100, "C"});
     24     symbols.insert({400, "CD"});
     25     symbols.insert({500, "D"});
     26     symbols.insert({900, "CM"});
     27     symbols.insert({1000, "M"});
     28     
     29     string numeral = "";
     30     while(num > 0){
     31         for(auto it = symbols.rbegin() ; it != symbols.rend() ; ++it ){
     32             if(num >= it->first){
     33                 num -= it->first;
     34                 numeral += it->second;
     35                 //This is inefficient because it needs to reloop each time it finds a val to make sure it does not iterate past a repeated value.
     36                 break;
     37             }
     38         }   
     39     }
     40     return numeral;
     41 }
     42 
     43 
     44 
     45 
     46 int main(){
     47 
     48 
     49 
     50 
     51     cout << intToRoman(134) << endl;
     52     return 0;
     53 
     54 }