//LeetCode-13-数组和map
class Solution {
public:
int romanToInt(string s) {
map<char,int> map;
map['M'] = 1000;
map['D'] = 500;
map['C'] = 100;
map['L'] = 50;
map['X'] = 10;
map['V'] = 5;
map['I'] = 1;
int temp = 0;
int sum =0;
for(int i=0;i<s.length();i++)
{
if(i!=0)
{
if(map[s[i]] > temp)
sum -= temp*2;
}
temp = map[s[i]];
sum += temp;
}
return sum;
}
};
52 - LeetCode-13 -罗马数字转整数_罗马数字转数字
最新推荐文章于 2024-01-03 17:21:57 发布