13. Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
这道题,是要我们将罗马数字转换成十进制数字,属于一道水题,就不过多的介绍了,直接上代码了。
java代码:
class Solution {
public int romanToInt(String s) {
int i, total, pre, cur;
total = charToInt(s.charAt(0));
for(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 -pre*2+cur;
}
}
return total;
}
public static int charToInt(char c) {
int data = 0;
switch (c) {
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;
}
}
网友评论