-
Notifications
You must be signed in to change notification settings - Fork 1
/
No12整数转罗马数字.java
47 lines (36 loc) · 1.26 KB
/
No12整数转罗马数字.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package leetCode.repository;
import java.util.HashMap;
import java.util.Map;
/**
* @author jiang
* @date 2023/6/26
*/
public class No12整数转罗马数字 {
public static void main(String[] args) {
System.out.println(new No12整数转罗马数字().intToRoman(58).equals("LVIII"));
System.out.println(new No12整数转罗马数字().intToRoman(1994).equals("MCMXCIV"));
}
public String intToRoman(int num) {
return new Solution1().intToRoman(num);
}
class Solution1 implements IntToRoman {
private static int[] table = new int[]{1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
private static String[] roma = new String[]{"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
@Override
public String intToRoman(int num) {
StringBuilder sb = new StringBuilder();
int pos = table.length - 1;
while (num > 0 && pos >= 0) {
for (int i = 0; i < num / table[pos] ; i++) {
sb.append(roma[pos]);
}
num %= table[pos];
pos--;
}
return sb.toString();
}
}
interface IntToRoman {
String intToRoman(int num);
}
}