-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.整数转罗马数字.java
49 lines (42 loc) · 1.21 KB
/
12.整数转罗马数字.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
48
/*
* @lc app=leetcode.cn id=12 lang=java
*
* [12] 整数转罗马数字
*/
// @lc code=start
class Solution {
private static final int[] NUMS = new int[] {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
public String intToRoman(int num) {
StringBuilder sb = new StringBuilder();
int count;
for (int i = 0; i < NUMS.length; i++) {
count = num / NUMS[i];
if (count > 0) {
for (int j = 0; j < count; j++) {
sb.append(intMap(NUMS[i]));
}
num = num - (count * NUMS[i]);
}
}
return sb.toString();
}
public static String intMap(int num) {
switch (num) {
case 1000: return "M";
case 900: return "CM";
case 500: return "D";
case 400: return "CD";
case 100: return "C";
case 90: return "XC";
case 50: return "L";
case 40: return "XL";
case 10: return "X";
case 9: return "IX";
case 5: return "V";
case 4: return "IV";
case 1: return "I";
default: return "";
}
}
}
// @lc code=end