This is the 12th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

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

Character values I 1 V 5 X 10 L 50 C 100 D 500 M 1000Copy the code

For example, the Roman numeral 2 is written as II, which is two ones side by side. 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:

Num = 3 output: "III"Copy the code

Example 2:

Input: num = 4 Output: "IV"Copy the code

Example 3:

Num = 9 output: "IX"Copy the code

Example 4:

Input: num = 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3Copy the code

Answer: The Roman numerals are represented by seven different single letters. For some special numbers, there are six corresponding ways to write four and nine, so 13 characters and numbers can be listed. Roman numerals start from left to right and are represented by the largest character, for example 14 should be represented by 10+4 as XIV rather than 5+5+4 as VVIV. By enumerating 13 corresponding relationships from large to small, the given integer num is subtracted from the maximum value of num, and then replaced with the corresponding Roman character, and then spliced into a string from left to right until num is reduced to 0

public String intToRoman(int num) {
    StringBuilder roman = new StringBuilder();
    // Enumerate the relationship between each number and the Roman character
    int[] key = {1000.900.500.400.100.90.50.40.10.9.5.4.1};
    String[] value = {"M"."CM"."D"."CD"."C"."XC"."L"."XL"."X"."IX"."V"."IV"."I"};
    // Start with large numbers from left to right
    for (int i = 0; i < 13; i++) {
        // Convert the current maximum number to the corresponding Roman character
        while(num >= key[i]) { num -= key[i]; roman.append(value[i]); }}return roman.toString();
}
Copy the code

Method 2: since num has a range of only [1,3999], we can list the Roman symbol for each digit [0,9] in the decimal, tens, hundreds, and thousands. In this way, we only need to obtain the first, tenth, hundred and thousand digits of num number, and then extract the corresponding Roman symbol of num number, and finally concatenate the number from left to right, from the highest to the lowest order.

class Solution {
    public String intToRoman(int num) {
        String[] thousands = {""."M"."MM"."MMM"};
        String[] hundreds = {""."C"."CC"."CCC"."CD"."D"."DC"."DCC"."DCCC"."CM"};
        String[] tenDigits = {""."X"."XX"."XXX"."XL"."L"."LX"."LXX"."LXXX"."XC"};
        String[] singleDigit = {""."I"."II"."III"."IV"."V"."VI"."VII"."VIII"."IX"};

        return thousands[num / 1000] + hundreds[num % 1000 / 100] + tenDigits[num % 100 / 10] + singleDigit[num % 10]; }}Copy the code