Advertisement
SergeyNasekin

Integer_To_Roman

Jun 5th, 2021
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. // I=1(73)   V=5(86)   X=10(88)  L=50(76)  C=100(67)  D=500(68)  M=1000(77)
  2. class Solution {
  3. public:
  4.     int romanToInt(string s) {
  5.     short sum = 0, counter = s.length();
  6.     for (char i = 0; i < counter ; ++i) {
  7.         switch (s[i]) {
  8.             case 73:{if(s[i+1]==86){sum+=4;++i;break;}
  9.                      if(s[i+1]==88){sum+=9;++i;break;}
  10.                      else{sum+=1;break;}}
  11.             case 86:{sum+=5;break;}
  12.             case 88:{if(s[i+1]==76){sum+=40;++i;break;}
  13.                     if(s[i+1]==67){sum+=90;++i;break;}
  14.                     else{sum+=10;break;}}
  15.             case 76:{sum+=50;break;}
  16.             case 67:{if(s[i+1]==68){sum+=400;++i;break;}
  17.                     if(s[i+1]==77){sum+=900;++i;break;}
  18.                     else{sum+=100;break;}}
  19.             case 68:{sum+=500;break;}
  20.             case 77:{sum+=1000;break;}
  21.         }
  22.     }
  23.     return sum;    
  24.     }
  25. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement