Roman to Integer

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

Solution: use <map> or <unordered_map> (clean)

 1 class Solution {
 2 public:
 3     int romanToInt(string s) {
 4         unordered_map<char,int> map;
 5         map['M'] = 1000;
 6         map['D'] = 500;
 7         map['C'] = 100;
 8         map['L'] = 50;
 9         map['X'] = 10;
10         map['V'] = 5;
11         map['I'] = 1;
12         
13         int res = 0;
14         for(int i = 0; i < s.size(); i++) {
15             if(i != s.size()-1 && map[s[i]] < map[s[i+1]]) 
16                 res -= map[s[i]];
17             else res += map[s[i]];
18         }
19         return res;
20     }
21 };
原文地址:https://www.cnblogs.com/zhengjiankang/p/3646831.html