/**
* @param {string} s
* @return {number}
*/
const romanMap = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
};
const romanToInt = function(s) {
let result = 0;
for (let i = 0; i < s.length; i++) {
const current = romanMap[s[i]];
const next = romanMap[s[i + 1]];
// Если текущее число меньше следующего, значит это случай вычитания (IV, IX и т.д.)
if (current < next) {
result -= current;
} else {
// В обычном случае просто прибавляем
result += current;
}
}
return result;
};
console.log(romanToInt('III')); //3
// console.log(romanToInt('LVIII')); //58
// console.log(romanToInt('LIVII')); //56
// console.log(romanToInt('MCMXCIV')); //1994
Code will run automatically after changes.
Code runs in an isolated environment.
Code runs in an isolated environment.