【LeetCode】13.罗马数字转整数
class Solution { public: int romanToInt(string s) { int ans=0; unordered_map<char,int> mp; mp['I']=1; mp['V']=5; mp['X']=10; mp['L']=50; mp['C']=100; mp['D']=500; mp['M']=1000; int i=0; for(i;i<s.size()-1;i++){ if(s[i]=='I'){ if(s[i+1]=='V') { ans+=4; i++; continue; } if(s[i+1]=='X'){ ans+=9; i++; continue; } } if(s[i]=='X'){ if(s[i+1]=='L') { ans+=40; i++; continue; } if(s[i+1]=='C'){ ans+=90; i++; continue; } } if(s[i]=='C'){ if(s[i+1]=='D') { ans+=400; i++; continue; } if(s[i+1]=='M'){ ans+=900; i++; continue; } } ans+=mp[s[i]]; } return ans+mp[s[i]]; } };