Question:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
public int romanToInt(String s) {
char[] arr = s.toCharArray();
int result = 0;
result += charRomantoInt(arr[0]);
for (int i = 1; i < arr.length; i++) {
if (charRomantoInt(arr[i]) > charRomantoInt(arr[i - 1])) {
result += charRomantoInt(arr[i]) - 2
* charRomantoInt(arr[i - 1]);
} else {
result += charRomantoInt(arr[i]);
}
}
return result;
}
private int charRomantoInt(char c) {
int result = 0;
switch (c) {
case 'I':
result = 1;
break;
case 'V':
result = 5;
break;
case 'X':
result = 10;
break;
case 'L':
result = 50;
break;
case 'C':
result = 100;
break;
case 'D':
result = 500;
break;
case 'M':
result = 1000;
break;
}
return result;
}