Question:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
An1_1:
Class solution {public: int tonum (char c) {Switch (c) {Case 'I': return 1; Case 'V': return 5; Case 'X ': return 10; Case 'l': return 50; Case 'C': return 100; Case 'D': return 500; Case 'M': Return 1000; default: return 0 ;}} int romantoint (string s) {// start typing your C/C ++ Solution Below // do not write int main () function int ret = 0; for (INT I = 0; I <S. size (); I ++) {if (I + 1 <S. size () {If (tonum (s [I]) <tonum (s [I + 1]) // high num <low num, then minus {ret-= tonum (s [I]);} else {RET + = tonum (s [I]);} else {RET + = tonum (s [I]) ;}} return ret ;}};
An1_2:
Class solution {public: int tonum (char c) {Switch (c) {Case 'I': return 1; Case 'V': return 5; Case 'X ': return 10; Case 'l': return 50; Case 'C': return 100; Case 'D': return 500; Case 'M': Return 1000; default: return 0 ;}} int romantoint (string s) {// start typing your C/C ++ Solution Below // do not write int main () function int ret = 0; int Len = S. size (); int pre = 1001; int cur; For (INT I = 0; I <Len; ++ I) {cur = tonum (s [I]); RET + = cur; If (cur> pre) {ret-= 2 * pre; // pre already add one time, now minus tow times} Pre = cur ;} return ret ;}};