-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntRomanConverter.java
69 lines (59 loc) · 1.85 KB
/
IntRomanConverter.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package org.sean.array;
import java.util.ArrayList;
import java.util.List;
/** 12. Integer to Roman */
public class IntRomanConverter {
public String intToRoman(int num) {
int base = 1;
StringBuilder builder = new StringBuilder();
List<String> list = new ArrayList<>();
while (num > 0) {
int dlt = num % 10;
list.add(basedToStr(dlt, base));
base *= 10;
num = num / 10;
}
int size = list.size();
for (int i = size - 1; i >= 0; i--) {
builder.append(list.get(i));
}
return builder.toString();
}
private String nChar(String ch, int count) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < count; i++) {
builder.append(ch);
}
return builder.toString();
}
String[] midChars1 = new String[] {"I", "V", "X"};
String[] midChars10 = new String[] {"X", "L", "C"};
String[] midChars100 = new String[] {"C", "D", "M"};
String[] midChars1000 = new String[] {"M", "", ""};
private String basedToStr(int i, int base) {
String[] chars = new String[3];
switch (base) {
case 1:
chars = midChars1;
break;
case 10:
chars = midChars10;
break;
case 100:
chars = midChars100;
break;
case 1000:
chars = midChars1000;
break;
}
if (i > 0 && i <= 3) return nChar(chars[0], i);
else if (i == 4) return chars[0] + chars[1]; // "XL";
else if (i == 5) return chars[1];
else if (i > 5 && i < 9) {
return chars[1] + nChar(chars[0], i - 5);
} else if (i == 9) {
return chars[0] + chars[2];
}
return "";
}
}