Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
The following is a supplement to Baidu's Roman numerals:
Single digit example
I, 1] II, 2] III, 3] IV, 4] V, 5] vi, 6] VII, 7] VIII, 8] IX, 9
· Dozens of examples
X, 10] Xi, 11] XII, 12] XIII, 13] XIV, 14] XV, 15] XVI, 16] XVII, 17] XVIII, 18] XIX, 19] XX, 20] XXI, 21] XXII, 22] XXIX, 29] XXX, 30] XXXIV, 34] XXXV, 35] XXXIX, 39] XL, 40] l, 50] Li, 51] LV, 55] lx, 60] LXV, 65] LXXX, 80] XC, 90] XCIII, 93] xcv, 95] XCVIII, 98] xcix, 99]
· Hundreds of digits
C, 100] CC, 200] CCC, 300] cd, 400] D, 500] DC, 600] DCC, 700] DCCC, 800] cm, 900] cmxcix, 999]
· Example of a thousand bits
M, 1000] MC, 1100] MCD, 1400] MD, 1500] MDC, 1600] mdclxvi, 1666] mdccclxxxviii, 1888] mdcccxcix, 1899] mcm, 1900] mcmlxxvi, 1976] mcmlxxxiv, 1984] mcmxc, 1990] Mm, 2000] mmmcmxcix, 3999]
Class solution {public: int romantoint (string s) {int Len = S. size (); Map <char, int> MDIC; MDIC ['I'] = 1; MDIC ['V'] = 5; MDIC ['X'] = 10; MDIC ['l'] = 50; MDIC ['C'] = 100; MDIC ['D'] = 500; MDIC ['M'] = 1000; int res = 0, I; for (I = 0; I <Len; I ++) {if (I <len-1 & (s [I] = 'I' & (s [I + 1] = 'V' | s [I + 1] = 'X ')) // or simply written as: if (I <len-1 & MDIC [s [I + 1]> MDIC [s [I])
| (S [I] = 'X' & (s [I + 1] = 'l' | s [I + 1] = 'C '))
| (S [I] = 'C' & (s [I + 1] = 'D' | s [I + 1] = 'M ') ))) {res + = (MDIC [s [I + 1]-MDIC [s [I]); I ++ ;} else {res + = MDIC [s [I] ;}// end for return res ;}};