您的位置:首页 > 其它

【LeetCode】13. Roman to Integer

2017-06-21 09:03 274 查看
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;

    }

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: