Showing
2 changed files
with
14 additions
and
37 deletions
| ... | @@ -3,38 +3,6 @@ | ... | @@ -3,38 +3,6 @@ |
| 3 | * @return {number} | 3 | * @return {number} |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | -/* | ||
| 7 | -var romanToInt = function (s) { | ||
| 8 | - const dict = { | ||
| 9 | - I: 1, | ||
| 10 | - V: 5, | ||
| 11 | - X: 10, | ||
| 12 | - L: 50, | ||
| 13 | - C: 100, | ||
| 14 | - D: 500, | ||
| 15 | - M: 1000 | ||
| 16 | - }; | ||
| 17 | - | ||
| 18 | - const sign = { | ||
| 19 | - I: {V: -1, X:-1}, | ||
| 20 | - X: {L: -1, C: -1}, | ||
| 21 | - C: {D: -1, M: -1} | ||
| 22 | - }; | ||
| 23 | - | ||
| 24 | - let num = 0; | ||
| 25 | - let last; | ||
| 26 | - | ||
| 27 | - s.split('').forEach(n => { | ||
| 28 | - let c = dict[n]; | ||
| 29 | - if (!c) { | ||
| 30 | - throw new Error('Wrong Roman Number'); | ||
| 31 | - } | ||
| 32 | - | ||
| 33 | - | ||
| 34 | - }) | ||
| 35 | -}; | ||
| 36 | -//*/ | ||
| 37 | - | ||
| 38 | // 定义罗马数字和对应的整数值映射表 | 6 | // 定义罗马数字和对应的整数值映射表 |
| 39 | const romanToIntegerMap = new Map([ | 7 | const romanToIntegerMap = new Map([ |
| 40 | ['I', 1], | 8 | ['I', 1], |
| ... | @@ -45,40 +13,49 @@ const romanToIntegerMap = new Map([ | ... | @@ -45,40 +13,49 @@ const romanToIntegerMap = new Map([ |
| 45 | ['D', 500], | 13 | ['D', 500], |
| 46 | ['M', 1000] | 14 | ['M', 1000] |
| 47 | ]); | 15 | ]); |
| 16 | + | ||
| 48 | /** | 17 | /** |
| 49 | * 将罗马数字转换为整数。 | 18 | * 将罗马数字转换为整数。 |
| 50 | * @param {string} romanNumeral - 罗马数字字符串。 | 19 | * @param {string} romanNumeral - 罗马数字字符串。 |
| 51 | * @returns {number} 转换后的整数值。 | 20 | * @returns {number} 转换后的整数值。 |
| 52 | */ | 21 | */ |
| 53 | function romanToInt(romanNumeral) { | 22 | function romanToInt(romanNumeral) { |
| 23 | + // 使用正则表达式验证罗马数字的格式是否正确 | ||
| 24 | + // M{0,4} - 最多4个M(1000) | ||
| 25 | + // (CM|CD|D?C{0,3}) - 百位数的表示:900(CM)或400(CD)或[500(D)]+[0-3个C] | ||
| 26 | + // (XC|XL|L?X{0,3}) - 十位数的表示:90(XC)或40(XL)或[50(L)]+[0-3个X] | ||
| 27 | + // (IX|IV|V?I{0,3}) - 个位数的表示:9(IX)或4(IV)或[5(V)]+[0-3个I] | ||
| 54 | const regex = /^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/; | 28 | const regex = /^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/; |
| 55 | if (!regex.test(romanNumeral)) { | 29 | if (!regex.test(romanNumeral)) { |
| 56 | throw new Error('Invalid Roman Numeral: ' + romanNumeral); | 30 | throw new Error('Invalid Roman Numeral: ' + romanNumeral); |
| 57 | } | 31 | } |
| 58 | 32 | ||
| 59 | - | ||
| 60 | let total = 0; | 33 | let total = 0; |
| 61 | for (let i = 0; i < romanNumeral.length; i++) { | 34 | for (let i = 0; i < romanNumeral.length; i++) { |
| 62 | const currentChar = romanNumeral[i]; | 35 | const currentChar = romanNumeral[i]; |
| 63 | const currentValue = romanToIntegerMap.get(currentChar); | 36 | const currentValue = romanToIntegerMap.get(currentChar); |
| 37 | + | ||
| 38 | + // 验证字符的有效性 | ||
| 64 | if (!currentValue) { | 39 | if (!currentValue) { |
| 65 | throw new Error('Wrong Roman Number'); | 40 | throw new Error('Wrong Roman Number'); |
| 66 | } | 41 | } |
| 67 | 42 | ||
| 68 | - // 如果下一个字符的值大于当前字符,那么需要从总和中减去当前值 | 43 | + // 处理特殊情况:如果当前数字小于下一个数字,则需要减去当前数字 |
| 44 | + // 例如:IV = 5 - 1 = 4,IX = 10 - 1 = 9 | ||
| 69 | if (i + 1 < romanNumeral.length && romanToIntegerMap.get(romanNumeral[i + 1]) > currentValue) { | 45 | if (i + 1 < romanNumeral.length && romanToIntegerMap.get(romanNumeral[i + 1]) > currentValue) { |
| 70 | total -= currentValue; | 46 | total -= currentValue; |
| 71 | } else { | 47 | } else { |
| 48 | + // 常规情况:直接将当前数字加到总和中 | ||
| 72 | total += currentValue; | 49 | total += currentValue; |
| 73 | } | 50 | } |
| 74 | 51 | ||
| 75 | - | 52 | + // 调试信息:显示每一步的计算过程 |
| 76 | console.info(i, '-', currentChar, ':', currentValue, '=', total); | 53 | console.info(i, '-', currentChar, ':', currentValue, '=', total); |
| 77 | } | 54 | } |
| 78 | 55 | ||
| 79 | return total; | 56 | return total; |
| 80 | } | 57 | } |
| 81 | 58 | ||
| 82 | -// 示例使用: | 59 | +// 测试代码:将罗马数字 'MCMXCIV' (1994) 转换为整数 |
| 83 | const romanInput = 'MCMXCIV'; | 60 | const romanInput = 'MCMXCIV'; |
| 84 | console.info(`The integer value of ${romanInput} is ${romanToInt(romanInput)}.`); | 61 | console.info(`The integer value of ${romanInput} is ${romanToInt(romanInput)}.`); | ... | ... |
-
Please register or login to post a comment