Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.
Solution:
1 Public classSolution {2 Public intRomantoint (String s) {3 if(S.length () ==0)return0;4 5map<character,integer> map =NewHashmap<character,integer>();6Map.put (' I ', 1);7Map.put (' V ', 5);8Map.put (' X ', 10);9Map.put (' L ', 50);TenMap.put (' C ', 100); OneMap.put (' D ', 500); AMap.put (' M ', 1000); - - intres = 0; the intindex = 0; - while(index<s.length ()) { - CharCur =S.charat (index); - if(index+1<s.length ()) { + CharNext = S.charat (index+1); - intVal1 =map.get (cur); + intVal2 =Map.get (next); A if(val1<val2) { atRes + = (val2-val1); -Index + = 2; -}Else { -Res + =Val1; -index++; - } in}Else { - intval =map.get (cur); toRes + =Val; +index++; - } the } * $ returnRes; Panax Notoginseng } -}
Leetcode-roman to Integer