[LeetCode] 13. Roman to Integer

 

 1 class Solution
 2 {
 3 public:
 4     int romanToInt(string s)
 5     {
 6         map<char, int> Map = {{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
 7         int sum = 0;
 8         for(int i = 0; i < s.length() - 1; i ++)
 9         {
10             if(Map[s[i]] < Map[s[i+1]])
11                 sum -= Map[s[i]];
12             else
13                 sum += Map[s[i]];
14         }
15         sum += Map[s[s.length() - 1]];
16         return sum;
17     }
18 };

 

posted @ 2017-02-20 16:09  Strawberry丶  阅读(159)  评论(0编辑  收藏  举报