思路:
public class Solution
{
private int getNum(char c)
{
switch (c)
{
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
default:
return 1000;
}
}
public int romanToInt(String s)
{
int sum = 0;
for (int i = 0; i < s.length(); i++)
{
if (i + 1 < s.length() && getNum(s.charAt(i)) < getNum(s.charAt(i + 1)))
{
sum -= getNum(s.charAt(i));
}
else
{
sum += getNum(s.charAt(i));
}
}
return sum;
}
}