leetcode

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

integer-to-romanV2Hash.cpp (1391B)


      1 #include <iostream>
      2 #include <map>
      3 using namespace std;
      4 
      5 //Leet Ratings
      6 //       Speed  Memory
      7 //Total  27ms    13MB    
      8 //Beats  6.76%   6.11%
      9 // Since I already know the refrence values I should use a vector of pairs because this method
     10 // sorts them when entering the refrences in the map which is not necessary.
     11 
     12 
     13 
     14 string intToRoman(int num) {
     15 
     16     //Create hashmap
     17     map <int ,string> symbols;
     18     symbols.insert({1, "I"});
     19     symbols.insert({4, "IV"});
     20     symbols.insert({5, "V"});
     21     symbols.insert({9, "IX"});
     22     symbols.insert({10, "X"});
     23     symbols.insert({40, "XL"});
     24     symbols.insert({50, "L"});
     25     symbols.insert({90, "XC"});
     26     symbols.insert({100, "C"});
     27     symbols.insert({400, "CD"});
     28     symbols.insert({500, "D"});
     29     symbols.insert({900, "CM"});
     30     symbols.insert({1000, "M"});
     31     string numeral = "";
     32     auto last_it = symbols.rbegin();
     33     bool no_it = false;
     34     while(num > 0){
     35         for(auto it = symbols.rbegin() ; it != symbols.rend() ; ++it ){
     36             
     37             if(no_it){
     38             it = last_it;
     39             no_it = false;
     40             }
     41 
     42             if(num >= it->first){
     43                 num -= it->first;
     44                 numeral += it->second;
     45                 no_it = true;
     46             }
     47             last_it = it;
     48         }
     49         
     50     }
     51 
     52     return numeral;
     53 }
     54 
     55 
     56 int main(){
     57 
     58 
     59 
     60 
     61     cout << intToRoman(134) << endl;
     62     return 0;
     63 
     64 }