-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0012_Integer_to_Roman.java
More file actions
28 lines (28 loc) · 990 Bytes
/
0012_Integer_to_Roman.java
File metadata and controls
28 lines (28 loc) · 990 Bytes
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
class Solution {
public String intToRoman(int num) {
int[] dividers = {1000, 500, 100, 50, 10, 5, 1};
String[] romans = {"M", "D", "C", "L", "X", "V", "I"};
StringBuilder sb = new StringBuilder();
int idx = 0;
while (num > 0) {
int target = num / dividers[idx];
num %= dividers[idx];
if (target == 4) {
if (sb.length() > 0 && sb.substring(sb.length() - 1).equals(romans[idx - 1])) {
sb.deleteCharAt(sb.length() - 1);
sb.append(romans[idx]);
sb.append(romans[idx - 2]);
} else {
sb.append(romans[idx]);
sb.append(romans[idx - 1]);
}
} else if (target > 0) {
for (int i = 0; i < target; i++) {
sb.append(romans[idx]);
}
}
idx++;
}
return sb.toString();
}
}