题目链接:https://leetcode-cn.com/problems/roman-to-integer/
题目描述:
题解:
class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int> umap;
umap.insert(pair<char, int>('I', 1));
umap.insert(pair<char, int>('V', 5));
umap.insert(pair<char, int>('X', 10));
umap.insert(pair<char, int>('L', 50));
umap.insert(pair<char, int>('C', 100));
umap.insert(pair<char, int>('D', 500));
umap.insert(pair<char, int>('M', 1000));
int ans = 0;
for(int i = 0; i < s.size();i++)
{
if(umap[s[i]] < umap[s[i+1]] )
{
ans -= umap[s[i]];
}else{
ans += umap[s[i]];
}
}
return ans;
}
};