knakul853

Untitled

Jul 20th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int romanToInt(string s) {
  4.        
  5.        
  6.         map<char,int>mp;
  7.         mp['I'] = 1;  mp['V'] = 5; mp['X'] = 10; mp['L'] = 50;
  8.         mp['C'] = 100; mp['D'] = 500; mp['M'] = 1000;
  9.        
  10.         int val = 0;
  11.        
  12.         for(int i=0;i<s.length();i++){
  13.            
  14.             if(s[i+1] && mp[s[i]] < mp[s[i+1]]){
  15.                 val+=mp[s[i+1]] - mp[s[i]] ;
  16.                 i++;
  17.             }
  18.             else{
  19.                 val+=mp[s[i]];
  20.             }
  21.         }
  22.         return val;
  23.        
  24.     }
  25. };
Add Comment
Please, Sign In to add comment