-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerToRoman.java
More file actions
38 lines (34 loc) · 1.45 KB
/
IntegerToRoman.java
File metadata and controls
38 lines (34 loc) · 1.45 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
/*
* https://leetcode.com/problems/integer-to-roman/
*/
public class IntegerToRoman {
public String intToRoman(int num) {
if (num == 0) {
return "";
}
int numDigits = (int) Math.floor(Math.log10(num) + 1);
int firstDigit = (int) (num / Math.pow(10, numDigits - 1));
return getRomanRepresentation(firstDigit, numDigits - 1) + intToRoman((int) (num % Math.pow(10, numDigits - 1)));
}
private String getRomanRepresentation(int digit, int index) {
return switch (index) {
case 0 -> getRomanRepresentation(digit, 'I', 'V', 'X');
case 1 -> getRomanRepresentation(digit, 'X', 'L', 'C');
case 2 -> getRomanRepresentation(digit, 'C', 'D', 'M');
case 3 -> getRomanRepresentation(digit, 'M', ' ', ' ');
default -> "";
};
}
private String getRomanRepresentation(int digit, char singleChar, char fiveChar, char tenChar) {
return switch (digit) {
case 0, 1, 2, 3 -> new String(new char[digit]).replace('\0', singleChar);
case 5, 6, 7, 8 -> fiveChar + getRomanRepresentation(digit - 5, singleChar, fiveChar, tenChar);
case 4 -> String.valueOf(singleChar) + fiveChar;
case 9 -> String.valueOf(singleChar) + tenChar;
default -> "";
};
}
public static void main(String[] args) {
System.out.println(new IntegerToRoman().intToRoman(1994)); // MCMXCIV
}
}