Skip to content

Commit 77c98ab

Browse files
committed
Add solution #12
1 parent cd5a188 commit 77c98ab

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

solutions/0012-integer-to-roman.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* https://leetcode.com/problems/integer-to-roman/
44
* Difficulty: Medium
55
*
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`.
78
*
89
* Symbol Value
910
* I 1
@@ -14,13 +15,15 @@
1415
* D 500
1516
* M 1000
1617
*
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`.
1921
*
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:
2427
*
2528
* - `I` can be placed before `V (5)` and `X (10)` to make 4 and 9.
2629
* - `X` can be placed before `L (50)` and `C (100)` to make 40 and 90.
@@ -34,7 +37,10 @@
3437
* @return {string}
3538
*/
3639
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+
};
3844

3945
return Object.entries(map).reduce((result, [letter, n]) => {
4046
result += letter.repeat(Math.floor(num / n));

0 commit comments

Comments
 (0)