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) { // Ⅰ(1)、X(10)、C(100)、M(1000)、V(5)、L(50)、D(500) int values[26] ={ 0,0,100,500,0,0,0,0,1,0,0,50,1000,0,0,0,0,0,0,0,0,5,0,10,0,0 }; int size = s.size(); int res = 0; for(int i=0;i<size;i++){ if(i+1<size){ if(values[s[i]-‘A‘] >= values[s[i+1]-‘A‘]) res += values[s[i]-‘A‘]; else res -= values[s[i]-‘A‘]; }else{ res += values[s[i]-‘A‘]; } } return res; } };
原文:http://www.cnblogs.com/zengzy/p/5024212.html