-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteger2Roman_12.java
More file actions
53 lines (49 loc) · 1.26 KB
/
Integer2Roman_12.java
File metadata and controls
53 lines (49 loc) · 1.26 KB
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
/**
* Roman Numbers:
* I, II, III, IV, V, VI, VII, VIII, IX, X.
* I 1
* V 5
* X 10
* L 50
* C 100
* D 500
* M 1,000
*/
/**
* 9 ---- map(i) map(i-1)
* 5-8 -- map(5i) map(i)...map(i)
* 4 ---- map(i) map(5i)
* 1-3 -- map(i)...map(i)
*/
public class Solution {
public String intToRoman(int num) {
@SuppressWarnings("serial")
final Map<Integer, String> map = new HashMap<Integer, String>() {{
put(1000, "M");
put(500, "D");
put(100, "C");
put(50, "L");
put(10, "X");
put(5, "V");
put(1, "I");
}};
StringBuilder sb = new StringBuilder();
int divisor = 1000;
while(num>0) {
int quot = num / divisor;
if (quot == 9) {
sb.append(map.get(divisor) + map.get(divisor*10));
quot = 0;
} else if (quot >= 5) { // XXXX not >5
sb.append(map.get(divisor*5));
quot -= 5;
} else if (quot == 4) {
sb.append(map.get(divisor) + map.get(divisor*5));
quot = 0;
}
for(;quot>0; quot--) sb.append(map.get(divisor));
num %= divisor; divisor /= 10;
}
return sb.toString();
}
}