Roman to Integer

时间:2022-12-15 03:45:22

Given a roman numeral, convert it to an integer.

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

  inline int c2n(char c) {
switch(c) {
case 'I': return ;
case 'V': return ;
case 'X': return ;
case 'L': return ;
case 'C': return ;
case 'D': return ;
case 'M': return ;
default: return ;
}
}
class Solution {
public:
int romanToInt(string s) { // Start typing your C/C++ solution below
// DO NOT write int main() function
int result=;
int len=s.length();
result=c2n(s[]);
for(int i =; i< len; i++)
{
if(c2n(s[i]) > c2n(s[i-]))
{
result +=(c2n(s[i]) - *c2n(s[i-]));
}
else
{
result += c2n(s[i]);
}
}
return result;
}
};