lintry

add #13

/**
* @param {string} s
* @return {number}
*/
/*
var romanToInt = function (s) {
const dict = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
};
const sign = {
I: {V: -1, X:-1},
X: {L: -1, C: -1},
C: {D: -1, M: -1}
};
let num = 0;
let last;
s.split('').forEach(n => {
let c = dict[n];
if (!c) {
throw new Error('Wrong Roman Number');
}
})
};
//*/
// 定义罗马数字和对应的整数值映射表
const romanToIntegerMap = new Map([
['I', 1],
['V', 5],
['X', 10],
['L', 50],
['C', 100],
['D', 500],
['M', 1000]
]);
/**
* 将罗马数字转换为整数。
* @param {string} romanNumeral - 罗马数字字符串。
* @returns {number} 转换后的整数值。
*/
function romanToInt(romanNumeral) {
const regex = /^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/;
if (!regex.test(romanNumeral)) {
throw new Error('Invalid Roman Numeral: ' + romanNumeral);
}
let total = 0;
for (let i = 0; i < romanNumeral.length; i++) {
const currentChar = romanNumeral[i];
const currentValue = romanToIntegerMap.get(currentChar);
if (!currentValue) {
throw new Error('Wrong Roman Number');
}
// 如果下一个字符的值大于当前字符,那么需要从总和中减去当前值
if (i + 1 < romanNumeral.length && romanToIntegerMap.get(romanNumeral[i + 1]) > currentValue) {
total -= currentValue;
} else {
total += currentValue;
}
console.info(i, '-', currentChar, ':', currentValue, '=', total);
}
return total;
}
// 示例使用:
const romanInput = 'MCMXCIV';
console.info(`The integer value of ${romanInput} is ${romanToInt(romanInput)}.`);