标签:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
class Solution { public: int romanToInt(string s) { int map[256]; map[‘I‘]=1;map[‘V‘]=5;map[‘X‘]=10;map[‘L‘]=50;map[‘C‘]=100;map[‘D‘]=500;map[‘M‘]=1000; int i=-1,len=s.length(),ans=0; while(++i<len){ if(i+1<len&&map[s[i]]<map[s[i+1]]){ ans-=map[s[i]]; } else ans+=map[s[i]]; } return ans; } };
标签:
原文地址:http://www.cnblogs.com/wqkant/p/5293235.html