Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.
Save the most typical, 1,5,10, and so on, and then if the left number is smaller than the right, subtract the left number, otherwise add
PackageRoman.to.Integer;ImportJava.util.HashMap;ImportJava.util.Map; Public classRomantointeger {Map<String,Integer> map=NewHashmap<string,integer>(); Public voidAddintkey,string value) { This. Map.put (value, key);} Public intRomantoint (String s) { This. Add (+, "M"); This. Add (+, "D"); This. Add (+, "C"); This. Add ("L"); This. Add (, "X"); This. Add (5, "V"); This. Add (1, "I"); intI=s.length ()-1; CharA=S.charat (i); String as=string.valueof (a); intresult= This. Map.get (AS); I--; while(i>=0){ Chartemp=S.charat (i); String TempS=string.valueof (temp); intCurr= This. Map.get (TempS); CharTempnext=s.charat (i+1); String tempnexts=string.valueof (Tempnext); intnext= This. Map.get (tempnexts); I--; if(next>Curr) {Result-=Curr; }Else{result+=Curr; } } returnresult; } Public Static voidMain (String args[]) {Romantointeger service=NewRomantointeger (); intA=service.romantoint ("IX"); System.out.println (a);}}
Leetcode Roman to Integer