标签:leetcode
class Solution {
public:
int romanToInt(string s) {
int map[128];
if(s.size()==0)
return 0;
map[‘I‘]=1;
map[‘V‘]=5;
map[‘X‘]=10;
map[‘L‘]=50;
map[‘C‘]=100;
map[‘D‘]=500;
map[‘M‘]=1000;
int res = map[s[0]];
for(int i=1;i<s.size();i++)
{
if(map[s[i]]<=map[s[i-1]])
res += map[s[i]];
else
res = res-2*map[s[i-1]]+map[s[i]];
}
return res;
}
};
标签:leetcode
原文地址:http://blog.csdn.net/majing19921103/article/details/45114759