const string roman[]={ "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM", "M", "MM", "MMM"}; const int integer[]={ 1,2,3,4,5,6,7,8,9, 10,20,30,40,50,60,70,80,90, 100,200,300,400,500,600,700,800,900, 1000,2000,3000}; class Solution { public: int romanToInt(string s) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(s=="") return NULL; int ret=0; int len=s.length(); int i,j=0; for(i=29;i>=0;i--) { string temp(s,j,roman[i].size()); if(temp==roman[i]) { ret+=integer[i]; j+=roman[i].size(); } } return ret; } };
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
思考:整数1-3999,暴力求解。