Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.
AC one of the fastest. Each type of letter represents a number. The small on the left side means right minus left, and small on the right side for right plus left. If IV represents 4, VI means 6.
I, I:1
V, V:5
X, X:10
L, l:50
C, c:100
D, d:500
M, m:1000
1 classSolution {2 Public:3 intRomantoint (strings) {4 intTimes =1;5 intresult =0;6 for(inti =0; I < s.length ()-1; i++){7 if(ToInt (s[i]) = = ToInt (s[i+1])) times++;8 Else if(ToInt (s[i+1]) >ToInt (S[i])) {9Result-= ToInt (S[i]) *Times ;TenTimes =1; One } A Else{ -Result + = ToInt (S[i]) *Times ; -Times =1; the } - } -Result + = ToInt (S[s.length ()-1]) *Times ; - returnresult; + } - intToInt (Charch) { + Switch(CH) { A Case 'I': Case 'I':return 1; at Case 'V': Case 'v':return 5; - Case 'X': Case 'x':return Ten; - Case 'L': Case 'L':return -; - Case 'C': Case 'C':return -; - Case 'D': Case 'D':return -; - Case 'M': Case 'm':return +; in } - } to};
Roman to Integer