12. Integer to Roman (integer to Roman numeral)
Link: https://leetcode-cn.com/problems/integer-to-roman /
Title:
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, D 500
M 1000
The number 2 is written as II, which means two parallel 1. 12 is written as XII, which means X + II. 27 is written as XXVII, which means XX + V + II.
Normally, the small number in Roman numerals is to the right of the large number. However, there are special cases. For example, 4 is not written as IIII, but IV. The number 1 is to the left of the number 5, and the number represented is equal to the number 4 obtained by subtracting the number 1 from the large number 5. Similarly, the number 9 is represented as IX. This special rule only applies to the following six situations:
I can be placed on the left of V(5) and X(10) to indicate 4 and 9.
X can be placed on the left of L (50) and C (100) to indicate 40 and 90.
C can be placed on the left of D (500) and M (1000) to indicate 400 and 900.
Given an integer, convert it to a Roman numeral. Ensure that the input is in the range of 1 to 3999.
Example 1:
Input: 3
Output: “III”
Example 2:
Input: 4
Output: ” IV”
Example 3:
Input: 9
Output: “IX”
Example 4:
Input: 58
Output: “LVIII”
Explanation: L = 50, V = 5, III = 3.
Example 5:
Input: 1994
Output: “MCMXCIV”
Explanation: M = 1000, CM = 900, XC = 90, IV = 4.
Thinking:
is not very troublesome, it is classification Screening, adding operations to strings, level 1000 and level 1 need to use a while loop, and other numbers can be directly judged once with if.
Code:
1 public static String intToRoman(int num) {
2 StringBuilder str = new StringBuilder();
3 while (num >= 1000) {
4 str.append("M");
5 num = num-1000;
6 }
7 if (num >= 900) {
8 str.append("CM");
9 num = num-900;
10 }
11 if (num >= 500) {
12 str.append("D");
13 num = num-500;
14 }
15 if (num >= 400 && num < 500) {
16 str.append("CD");
17 num = num-400;
18 }
19 while (num >= 100) {
20 str.append("C");
21 num = num-100;
22 }
23
24 if (num >= 90) {
25 str.append("XC");
26 num = num-90;
27 }
28 if (num >= 50) {
29 str.append("L");
30 num = num-50;
31 }
32 if (num >= 40 && num < 50) {
33 str.append("XL");
34 num = num-40;
35 }
36 while (num >= 10) {
37 str.append("X");
38 num = num-10;
39 }
40
41 if (num >= 9) {
42 str.append("IX");
43 num = num-9;
44 }
45 if (num >= 5) {
46 str.append("V");
47 num = num-5;
48 }
49 if (num >= 4 && num < 5) {
50 str.append("IV");
51 num = num-4;
52 }
53 while (num >= 1) {
54 str.append("I");
55 num = num-1;
56 }
57 return str.toString();
58 }
1 public static String intToRoman(int num) {
2 StringBuilder str = new StringBuilder();
3 while (num >= 1000) {
4 str.append("M");
5 num = num-1000;
6 }
7 if (num >= 900) {
8 str.append("CM");
9 num = num-900;
10 }
11 if (num >= 500) {
12 str.append("D");
13 num = num-500;
14 }
15 if (num >= 400 && num < 500) {
16 str.append("CD");
17 num = num-400;
18 }
19 while (num >= 100) {
20 str.append("C");
21 num = num-100;
22 }
23
24 if (num >= 90) {
25 str.append("XC");
26 num = num-90;
27 }
28 if (num >= 50) {
29 str.append("L");
30 num = num-50;
31 }
32 if (num >= 40 && num < 50) {
33 str.append("XL");
34 num = num-40;
35 }
36 while (num >= 10) {
37 str.append("X");
38 num = num-10;
39 }
40
41 if (num >= 9) {
42 str.append("IX");
43 num = num-9;
44 }
45 if (num >= 5) {
46 str.append("V");
47 num = num-5;
48 }
49 if (num >= 4 && num < 5) {
50 str.append("IV");
51 num = num-4;
52 }
53 while (num >= 1) {
54 str.append("I");
55 num = num-1;
56 }
57 return str.toString();
58 }