LeeCode-Roman to Integer

时间:2021-05-20 04:08:25

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

 class Solution {
public:
int romanToInt(string s) {
int length = s.length();
if(length <) return ;
map<char,int> m;
m['I'] = ;
m['V'] = ;
m['X'] = ;
m['L'] = ;
m['C'] = ;
m['D'] = ;
m['M'] = ;
int i = length-;
int sum = m[s[i--]];
while(i>=)
if(m[s[i+]] > m[s[i]])
sum -= m[s[i--]];
else
sum += m[s[i--]];
return sum;
}
};