Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

I. Title Description:

  1. Integer to Roman numerals – Medium difficulty

Roman numerals contain the following seven characters: I, V, X, L, C, D and M.

Character value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, the Roman numeral 2 is written as II, which is the parallel 1. Write XII as X + II. Write XXVII as XX + V + II.

Usually, the smaller Roman numerals are to the right of the larger numerals. But there are exceptions, for example, 4 is not written as IIII, it’s written as IV. The number 1 is to the left of the number 5 and represents the number 4 when the larger number 5 decreases by 1. Similarly, the number 9 represents IX. This particular rule applies only to the following six situations:

I can be put to the left of V (5) and X (10) to represent 4 and 9. X can be placed to the left of L (50) and C (100) to represent 40 and 90. C can be put to the left of D (500) and M (1000) to represent 400 and 900. Give you an integer and convert it to Roman numerals.

 

Example 1:

Input: num = 3 Output: “III”

Input: num = 4 Output: “IV” Example 3:

Input: num = 9 Output: “IX”

Num = 58 output: “LVIII” Explanation: L = 50, V = 5, III = 3 Example 5:

Num = 1994 Output: “MCMXCIV” Explanation: M = 1000, CM = 900, XC = 90, IV = 4.

Tip:

1 <= num <= 3999

Ii. Topic and Idea Analysis:

A. that B. that C. that D. that

1 I writing

2 writing II

3 writing III

4 write IV

Writing 5 V

6 write VI

7 writing VII

8 writing VIII

9 writing IX

Writing 10 X

11 writing XI

12 assist in writing

XIII 13 writing

XIV 14 writing

.

So what does it look like to switch in your own way? Such as: 2888

So let’s write 2000 is 1000 plus 1000, which is MM

800 is 500 plus 300, DCCC

80 is plus 50 plus 30, which is LXXX

8 is plus 5 plus 3, which is VIII

The result is spliced together.

It’s so long…

It can be seen that: those less than 10 decide to increase or decrease based on 5. The words of decrease are put on the left side of 5, and the words of increase are put on the right side of 5. According to this standard, the base values of 1-3000 can be listed


1Writing I10Writing X100Writing C1000M writing2Writing II20The writing of XX200Writing the CC2000Writing the MM3Writing III30Writing XXX300Writing CCC3000Writing MMM4Writing IV40Writing XL400Writing a CD5Writing V50Writing the L500Write D6Writing VI60Writing LX600Writing a DC7Writing VII70Writing LXX700Writing DCC8Writing VIII80Writing LXXX800Writing DCCC9Writing IX90Writing XC900Writing the CMCopy the code

Now that the result is concatenated and has a base value, it’s easy to write the code I want:

Iii. Code:

The code implementation is as follows:

/ * * *@param {number} num
 * @return {string}* /
var intToRoman = function(num) {
    let obj = {
        4: ['M'.'MM'.'MMM'].3: ['C'.'CC'.'CCC'.'CD'.'D'.'DC'.'DCC'.'DCCC'.'CM'].2: ['X'.'XX'.'XXX'.'XL'.'L'.'LX'.'LXX'.'LXXX'.'XC'].1: ['I'.'II'.'III'.'IV'.'V'.'VI'.'VII'.'VIII'.'IX']}let arr = [...num.toString()].reverse() // select * from the single digit
    let res = ' '
    arr.forEach((item, index) = >{
        if(+item>0) {// If the current digit is not 0, the inverted digit should take effect when it is 0. For example, 10 takes the ten digit, not the ones digit
            res = obj[index+1][+item-1] + res  // Take the Roman numeral corresponding to the current number, and concatenate the resulting value in front of the result}})return res
}




Copy the code

Iv. Summary:

The difficulty of this problem is ok, according to the train of thought, the train of thought can be translated into code.

The first commit failed because I forgot to judge the 0 in the number, and the second commit failed because the initial value in the OBj object was carelessly written wrong.

A little bit more careful, a little more careful examination of logic.

Come on!