Roman numerals are a system of numerical notation used in ancient Rome. They are represented by combinations of the following seven symbols:
-
I for 1
-
V for 5
-
X for 10
-
L for 50
-
C for 100
-
D for 500
-
M for 1000
In Roman numerals, symbols are usually written from largest to smallest from left to right. However, there are a few exceptions where a smaller numeral precedes a larger numeral to indicate subtraction. For example:
-
IV represents 4 (5 - 1).
-
IX represents 9 (10 - 1).
-
XC represents 90 (100 - 10).
-
CD represents 400 (500 - 100).
-
CM represents 900 (1000 - 100).
Your task is to write a function that converts a given Roman numeral into its integer value.
Input: s = "III"
Output: 3
Input: s = "LVIII"
Output: 58
Input: s = "MCMXCIV"
Output: 1994
function romanToIntBasic(s) {
const romanToValue = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
};
let total = 0;
const length = s.length;
for (let i = 0; i < length; i++) {
const current = romanToValue[s[i]];
const next = i < length - 1 ? romanToValue[s[i + 1]] : 0;
if (current < next) {
total -= current;
} else {
total += current;
}
}
return total;
}function romanToIntTwoPass(s) {
const romanToValue = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
};
let total = 0;
const length = s.length;
for (let i = 0; i < length; i++) {
total += romanToValue[s[i]];
}
for (let i = 0; i < length - 1; i++) {
if (romanToValue[s[i]] < romanToValue[s[i + 1]]) {
total -= 2 * romanToValue[s[i]];
}
}
return total;
}function romanToIntMap(s) {
const romanToValue = new Map([
['I', 1],
['V', 5],
['X', 10],
['L', 50],
['C', 100],
['D', 500],
['M', 1000]
]);
let total = 0;
const length = s.length;
for (let i = 0; i < length; i++) {
const current = romanToValue.get(s[i]);
const next = i < length - 1 ? romanToValue.get(s[i + 1]) : 0;
if (current < next) {
total -= current;
} else {
total += current;
}
}
return total;
}function romanToIntRecursive(s) {
const romanToValue = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
};
function helper(index) {
if (index >= s.length) return 0;
const current = romanToValue[s[index]];
const next = index < s.length - 1 ? romanToValue[s[index + 1]] : 0;
if (current < next) {
return -current + helper(index + 1);
} else {
return current + helper(index + 1);
}
}
return helper(0);
}function romanToIntSwitch(s) {
function romanToValue(char) {
switch (char) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
}
}
let total = 0;
const length = s.length;
for (let i = 0; i < length; i++) {
const current = romanToValue(s[i]);
const next = i < length - 1 ? romanToValue(s[i + 1]) : 0;
if (current < next) {
total -= current;
} else {
total += current;
}
}
return total;
}function romanToIntFunctionObject(s) {
const romanToValue = {
I: () => 1,
V: () => 5,
X: () => 10,
L: () => 50,
C: () => 100,
D: () => 500,
M: () => 1000
};
let total = 0;
const length = s.length;
for (let i = 0; i < length; i++) {
const current = romanToValue[s[i]]();
const next = i < length - 1 ? romanToValue[s[i + 1]]() : 0;
if (current < next) {
total -= current;
} else {
total += current;
}
}
return total;
}function romanToIntLookupArray(s) {
const romanToValue = ['I', 'V', 'X', 'L', 'C', 'D', 'M'];
const values = [1, 5, 10, 50, 100, 500, 1000];
function getValue(char) {
return values[romanToValue.indexOf(char)];
}
let total = 0;
const length = s.length;
for (let i = 0; i < length; i++) {
const current = getValue(s[i]);
const next = i < length - 1 ? getValue(s[i + 1]) : 0;
if (current < next) {
total -= current;
} else {
total += current;
}
}
return total;
}function romanToIntReduce(s) {
const romanToValue = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
};
return s.split('').reduce((total, current, index, arr) => {
const value = romanToValue[current];
const next = index < arr.length - 1 ? romanToValue[arr[index + 1]] : 0;
return total + (value < next ? -value : value);
}, 0);
}function romanToIntStringIndex(s) {
const romanToValue = 'IVXLCDM';
const values = [1, 5, 10, 50, 100, 500, 1000];
function getValue(char) {
return values[romanToValue.indexOf(char)];
}
let total = 0;
const length = s.length;
for (let i = 0; i < length; i++) {
const current = getValue(s[i]);
const next = i < length - 1 ? getValue(s[i + 1]) : 0;
if (current < next) {
total -= current;
} else {
total += current;
}
}
return total;
}function romanToIntObject(s) {
const romanToValue = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
};
let total = 0;
const length = s.length;
for (let i = 0; i < length; i++) {
const current = romanToValue[s[i]];
const next = i < length - 1 ? romanToValue[s[i + 1]] : 0;
if (current < next) {
total -= current;
} else {
total += current;
}
}
return total;
}