Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.
Subscribe to see which companies asked this question
Ⅰ (1) Ⅴ (5) Ⅹ (Ten) L (+) C (+) D (+) M (1000)
Rule: At the back of a large number as a addend, in the front of the large number as a meiosis
such as: ⅲ=3,ⅳ=4,ⅵ=6,ⅹⅸ=19,ⅹⅹ=20,ⅹlⅴ=45,mcmⅹⅹc=1980
Starting from the lowest bit, if the previous number is larger than it, let the previous value precede it, and if it is smaller, let the current value minus the previous value
intRomantoint (strings) {if(s.length () = =0)return 0; intLen =s.length (); Unordered_map<Char,int>map; Map.insert (Make_pair ('I',1)); Map.insert (Make_pair ('V',5)); Map.insert (Make_pair ('X',Ten)); Map.insert (Make_pair ('L', -)); Map.insert (Make_pair ('C', -)); Map.insert (Make_pair ('D', -)); Map.insert (Make_pair ('M', +)); intresult = map.at (s.at (Len-1)); intPivot =result; for(inti = len-2; I >=0; i--) { intCurr =map.at (s.at (i)); if(Curr >=pivot) {Result+=Curr; } Else{result-=Curr; } Pivot=Curr; } returnresult;}
Roman to Integer Leetcode