Description:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
题目分析:
题目要求罗马数字转化为整型数字,查询罗马特殊字符:
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
Solution:
class Solution {
public:
int romanToInt(string s) {
int sum=0;
int strLen = s.size();
map<char,int> myMap;
myMap['I']=1;
myMap['V']=5;
myMap['X']=10;
myMap['L']=50;
myMap['C']=100;
myMap['D']=500;
myMap['M']=1000;
//vector<int> myVec(4,0);
int i=0;
int j;
while( i < strLen )
{
while(i<strLen&&myMap[s[i]] >= myMap[s[i+1]])
{
sum = sum + myMap[s[i]];
i++;
}
while(i<strLen&&myMap[s[i]] < myMap[s[i+1]])
{
sum = sum - myMap[s[i]];
i++;
}
}
return sum;
}
};