Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
public class Solution {
public int romanToInt(String s) {
if(s == null || s.length() == 0) return 0;
int num = 0;
for(int i = 0; i < s.length(); i++){
switch(s.charAt(i)){
case 'M':
num += 1000;
break;
case 'D':
num += 500;
break;
case 'C':
if(i < s.length() - 1 && (s.charAt(i + 1) == 'M' || s.charAt(i + 1) == 'D')){
num -= 100;
} else {
num += 100;
}
break;
case 'L':
num += 50;
break;
case 'X':
if(i < s.length() - 1 && (s.charAt(i + 1) == 'L' || s.charAt(i + 1) == 'C')){
num -= 10;
} else {
num += 10;
}
break;
case 'V':
num += 5;
break;
case 'I':
if(i < s.length() - 1 && (s.charAt(i + 1) == 'X' || s.charAt(i + 1) == 'V')){
num -= 1;
} else {
num += 1;
}
break;
default:
num += 0;
}
}
return num;
}
}