https://leetcode.com/problems/roman-to-integer/
Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.
1 ImportJava.util.HashMap;2 ImportJava.util.Map;3 4 Public classSolution {5 Public Static intRomantoint (String s) {6Map<string,integer>roman2int=Createmap ();7 intAns=0;8 intstart=0;intEnd=0;9 intlen=s.length ();Ten while(end<Len) { Onestart=end;end++; AString t= "" +S.charat (start); - while(End<len&&roman2int.containskey (t+S.charat (end))) { -t+=S.charat (end); theend++; - } -ans+=Roman2int.get (t); - } + returnans; - } + Public StaticMap<string,integer>Createmap () { AMap<string,integer>roman2int=NewHashMap (); atRoman2int.put ("I", 1); -Roman2int.put ("II", 2); -Roman2int.put ("III", 3); -Roman2int.put ("IV", 4); -Roman2int.put ("V", 5); -Roman2int.put ("VI", 6); inRoman2int.put ("VII", 7); -Roman2int.put ("VIII", 8); toRoman2int.put ("IX", 9); +Roman2int.put ("X", 10); -Roman2int.put ("XX", 20); theRoman2int.put ("XXX", 30); *Roman2int.put ("XL", 40); $Roman2int.put ("L", 50);Panax NotoginsengRoman2int.put ("LX", 60); -Roman2int.put ("LXX", 70); theRoman2int.put ("LXXX", 80); +Roman2int.put ("XC", 90); ARoman2int.put ("C", 100); theRoman2int.put ("CC", 200); +Roman2int.put ("CCC", 300); -Roman2int.put ("CD", 400); $Roman2int.put ("D", 500); $Roman2int.put ("DC", 600); -Roman2int.put ("DCC", 700); -Roman2int.put ("DCCCC", 800); theRoman2int.put ("CM", 900); -Roman2int.put ("M", 1000);WuyiRoman2int.put ("MM", 2000); theRoman2int.put ("MMM", 3000); - returnRoman2int; Wu } - Public Static voidMain (String[]args) { AboutSystem.out.println (Romantoint ("MDLXVII")); $ } -}
Roman to Integer