133. roman numerals to integer

JavaScript
Is this helpful ?

easy  - accepted / - tried

Roman numerals are represented by combinations of following seven symbols, each with a fixed integer value.

Symbol I V X L C D M
Value 1 5 10 50 100 500 1000

For Standard form, subtractive notation is used, meaning 4 is IV rather than IIII, 9 is IX rather than VIIII. Same rule applies to 40(XL) and 900(CM) .etc.

Simply speaking, the roman numerals in standard form follow these rules.

  1. symbols are listed from highest to lowest, from left to right
  2. from left to right, if the next symbol value is bigger than current one, it means subtracting, otherwise adding.

Please implement romanToInteger(). The input are all valid strings.


romanToInteger('CXXIII')
// 123

romanToInteger('MCMXCIX')
// 1999

romanToInteger('MMMCDXX')
// 3420

Let's try to solve this problem within 10 minutes.

Discuss(47)