13 Roman to Integer(罗马数字转int Easy)

题目意思:罗马数字转int

思路:字符串从最后一位开始读,IV:+5-1

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

 

posted @ 2015-06-17 21:18  影翕  阅读(235)  评论(0编辑  收藏  举报