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) {
if(s.size() == 0) return 0;
int num=0;
for(size_t i=0;i<s.size();i++){
char x=s[i];
switch(x){
case 'I': num+=1;break;
case 'V':
num=(i-1>=0 && s[i-1] == 'I')?num+3:num+5;
break;
case 'X':
num=(i-1>=0 && s[i-1] == 'I') ? num+8 : num+10;
break;
case 'L':
num=(i-1>=0 && s[i-1] == 'X') ? num+30 : num+50;
break;
case 'C':
num=(i-1>=0 && s[i-1] == 'X') ? num+80 : num+100;
break;
case 'D':
num=(i-1>=0 && s[i-1] == 'C') ? num+300 : num+500;
break;
case 'M':
num=(i-1>=0 && s[i-1] == 'C') ? num+800 : num+1000;
break;
default: break;
}
}
if(num >3999) return 0;
return num;
}
};