According to Roman numerals, the case of 2 or 3 consecutive letters must have a large or equal number on the left. That is, III or VII.
There is no Iiv this notation, that is, there is no continuous subtraction of two decimal notation.
Public classSolution { Public intRomantoint (String s) {if(S.length () <1) return0; intSize=s.length (); intRes=0; Map<Character,Integer> mp=NewHashmap<character,integer>(); //correct usage of hash tableMp.put (' I ', 1); Mp.put (' V ', 5); Mp.put (' X ', 10); Mp.put (' L ', 50); Mp.put (' C ', 100); Mp.put (' D ', 500); Mp.put (' M ', 1000); //according to Roman numerals, the case of 2 or 3 consecutive letters must have a large or equal number on the left. That is, III or VII .//There is no Iiv this notation, that is, there is no continuous subtraction of two decimal notation. //Res=mp.get (S.charat (size-1)); for(inti=size-2;i>=0;i--) { intt=Mp.get (S.charat (i)); intT_r=mp.get (S.charat (i+1)); if(t>=t_r) Res+=T; ElseRes-=T; } returnRes; }}
Roman to Integer