Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
看着roman numeral是一头雾水, google过后知道是罗马数字, 就是I, II, III, IV, V, VI, VII, VIII, IX, X... 写report的时候有用到可是鬼知道有神马规律! 于是这题连思考的时间都省了, 只能上网找答案.
计数规则:
- 相同的数字连写,所表示的数等于这些数字相加得到的数,例如:III = 3
- 小的数字在大的数字右边,所表示的数等于这些数字相加得到的数,例如:VIII = 8
- 小的数字,限于(I、X和C)在大的数字左边,所表示的数等于大数减去小数所得的数,例如:IV = 4
- 正常使用时,连续的数字重复不得超过三次
- 在一个数的上面画横线,表示这个数扩大1000倍(本题只考虑3999以内的数,所以用不到这条规则)
public class Solution {
public int romanToInt(String s) {
int pre = 0;
int cur = 0;
int total = charToInt(s.charAt(0));
for(int i=1; i<s.length(); i++){
pre = charToInt(s.charAt(i-1));
cur = charToInt(s.charAt(i));
if(cur<=pre) total+=cur;
else total = total+cur-2*pre;
}
return total;
}
public int charToInt(char a){
int data = 0;
switch (a){
case 'I':
data = 1;
break;
case 'V':
data = 5;
break;
case 'X':
data = 10;
break;
case 'L':
data = 50;
break;
case 'C':
data = 100;
break;
case 'D':
data = 500;
break;
case 'M':
data = 1000;
break;
}
return data;
}
}