題目


Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

SymbolValue
I1
V5
X10
L50
C100
D500
M1000

For example, 2 is written as II in Roman numeral, just two one’s added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given an integer, convert it to a roman numeral.

Example 1:

1Input: num = 3
2Output: "III"
3Explanation: 3 is represented as 3 ones.

Example 2:

1Input: num = 58
2Output: "LVIII"
3Explanation: L = 50, V = 5, III = 3.

Example 3:

1Input: num = 1994
2Output: "MCMXCIV"
3Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Constraints:

  • 1 <= num <= 3999

我的思路


除了千位數,每一個位數先看有沒有 9 和 4 ,如果有就直接 Append 對應的羅馬數字。如果沒有就看有沒有大於五,有就將五的數字羅馬數字加上去,然後再把剩下一加上。

例如:

  • 80: 由於 80 的十位數為 8,那就先加上 50(L) ,然後 8 - 5 = 3 ,就代表要加上 3 個 10(X),最後等於 LXXX

程式碼


 1class Solution {
 2    public String intToRoman(int num) {
 3        StringBuilder result = new StringBuilder();
 4        appendThousandsRoman(num, result);
 5        appendHundredsRoman(num, result);
 6        appendTensRoman(num, result);
 7        appendDigitsRoman(num, result);
 8        return result.toString();
 9    }
10    
11    public void appendThousandsRoman(int num, StringBuilder result) {
12        int nums_of_thousands_roman = num / 1000;
13        for(int i = 0; i < nums_of_thousands_roman; i++) {
14            result.append("M");
15        }
16    }
17    
18    public void appendHundredsRoman(int num, StringBuilder result) {
19        int nums_of_hundreds_roman = (num % 1000) / 100;
20        if (nums_of_hundreds_roman == 9) {
21            result.append("CM");
22            return;
23        } else if (nums_of_hundreds_roman == 4) {
24            result.append("CD");
25            return;
26        } else if (nums_of_hundreds_roman >= 5) {
27            result.append("D");
28            nums_of_hundreds_roman -= 5;
29        }
30
31        for(int i = 0; i < nums_of_hundreds_roman; i++) {
32            result.append("C");
33        }
34    }
35
36    public void appendTensRoman(int num, StringBuilder result) {
37        int nums_of_hundreds_roman = (num % 100) / 10;
38        if (nums_of_hundreds_roman == 9) {
39            result.append("XC");
40            return;
41        } else if (nums_of_hundreds_roman == 4) {
42            result.append("XL");
43            return;
44        } else if (nums_of_hundreds_roman >= 5) {
45            result.append("L");
46            nums_of_hundreds_roman -= 5;
47        }
48
49        for(int i = 0; i < nums_of_hundreds_roman; i++) {
50            result.append("X");
51        }
52    }
53    
54    public void appendDigitsRoman(int num, StringBuilder result) {
55        int nums_of_hundreds_roman = num % 10;
56        if (nums_of_hundreds_roman == 9) {
57            result.append("IX");
58            return;
59        } else if (nums_of_hundreds_roman == 4) {
60            result.append("IV");
61            return;
62        } else if (nums_of_hundreds_roman >= 5) {
63            result.append("V");
64            nums_of_hundreds_roman -= 5;
65        }
66
67        for(int i = 0; i < nums_of_hundreds_roman; i++) {
68            result.append("I");
69        }
70    }
71}