1 var roman = { 2 "I": 1, 3 "V": 5, 4 "X": 10, 5 "L": 50, 6 "C": 100, 7 "D": 500, 8 "M": 1000 9 }; 10 11 var romanToInt = function(s) { 12 if (s.length < 1) { 13 return 0; 14 } 15 16 for (var i = s.length - 1; i >= 0; i--) { 17 if (i == s.length - 1) { 18 var res = roman[s[i]]; 19 continue; 20 } 21 22 if (roman[s[i]] >= roman[s[i + 1]]) { 23 res += roman[s[i]]; 24 } else { 25 res -= roman[s[i]]; 26 } 27 } 28 29 return res; 30 };