|
3 | 3 | * https://leetcode.com/problems/integer-to-roman/ |
4 | 4 | * Difficulty: Medium |
5 | 5 | * |
6 | | - * Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`. |
| 6 | + * Roman numerals are represented by seven different symbols: `I`, `V`, `X`, |
| 7 | + * `L`, `C`, `D` and `M`. |
7 | 8 | * |
8 | 9 | * Symbol Value |
9 | 10 | * I 1 |
|
14 | 15 | * D 500 |
15 | 16 | * M 1000 |
16 | 17 | * |
17 | | - * For example, `2` is written as `II` in Roman numeral, just two one's added together. `12` is written |
18 | | - * as `XII`, which is simply `X + II`. The number `27` is written as `XXVII`, which is `XX + V + II`. |
| 18 | + * For example, `2` is written as `II` in Roman numeral, just two one's |
| 19 | + * added together. `12` is written as `XII`, which is simply `X + II`. |
| 20 | + * The number `27` is written as `XXVII`, which is `XX + V + II`. |
19 | 21 | * |
20 | | - * Roman numerals are usually written largest to smallest from left to right. However, the numeral for |
21 | | - * four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five |
22 | | - * we subtract it making four. The same principle applies to the number nine, which is written as `IX`. |
23 | | - * There are six instances where subtraction is used: |
| 22 | + * Roman numerals are usually written largest to smallest from left to right. |
| 23 | + * However, the numeral for four is not `IIII`. Instead, the number four is |
| 24 | + * written as `IV`. Because the one is before the five we subtract it making |
| 25 | + * four. The same principle applies to the number nine, which is written as |
| 26 | + * `IX`. There are six instances where subtraction is used: |
24 | 27 | * |
25 | 28 | * - `I` can be placed before `V (5)` and `X (10)` to make 4 and 9. |
26 | 29 | * - `X` can be placed before `L (50)` and `C (100)` to make 40 and 90. |
|
34 | 37 | * @return {string} |
35 | 38 | */ |
36 | 39 | var intToRoman = function(num) { |
37 | | - const map = { M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1 }; |
| 40 | + const map = { |
| 41 | + M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, |
| 42 | + L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1 |
| 43 | + }; |
38 | 44 |
|
39 | 45 | return Object.entries(map).reduce((result, [letter, n]) => { |
40 | 46 | result += letter.repeat(Math.floor(num / n)); |
|
0 commit comments