Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.
Conversion of Roman numerals to Arabic numerals
I 1
X 10
C 100
M 1000
V 5
L 50
D 500
Small numbers in front of large numbers, minus small numbers for large numbers, and small numbers for large numbers after large numbers
Public classSolution { Public intRomantoint (strings) {intresult =0; Char[] Chlist =S.tochararray (); Dictionary<Char,int> romadic =Newdictionary<Char,int>(); Romadic.add ('I',1); Romadic.add ('X',Ten); Romadic.add ('C', -); Romadic.add ('M', +); Romadic.add ('V',5); Romadic.add ('L', -); Romadic.add ('D', -); intFormer, latter =0; for(inti =0; I < chlist.length-1; i++) {Former=0; Latter=0; Romadic.trygetvalue (Chlist[i], outformer); Romadic.trygetvalue (Chlist[i+1], outlatter); if(Former <latter) {Result-=former; } Else{result+=former; } } intLast =0; Romadic.trygetvalue (Chlist.last (), outLast ); Result+=Last ; returnresult; }}
13_roman to Integer