leetcode13Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
这个题目,,不说了,,,去百度罗马数字的一些规则,,,比如C在D或者M前面是-100的意思。上代码。。。
class Solution {
public:
int romanToInt(string s) {
int sum = 0;
char pre = ' ';
for (string::iterator it = s.begin(); it != s.end(); ++it)
{
switch (*it)
{
case 'M':
sum += 1000;
break;
case 'D':
sum += 500;
break;
case 'C':
if (*(it +1) == 'M' || *(it+1) == 'D')
sum -= 100;
else
sum += 100;
break;
case 'L':
sum += 50;
break;
case 'X':
if (*(it +1) == 'L' || *(it+1) == 'C')
sum -= 10;
else
sum += 10;
break;
case 'V':sum += 5;
break;
case 'I':
if (*(it +1) == 'V' || *(it +1) == 'X')
sum -= 1;
else
sum += 1;
break;
}
}
return sum;
}
};