-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode-12.java
More file actions
70 lines (60 loc) · 1.85 KB
/
Copy pathLeetcode-12.java
File metadata and controls
70 lines (60 loc) · 1.85 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Solution {
public String intToRoman(int num) {
StringBuilder sb = new StringBuilder();
while(num != 0){
int n = num;
int cnt = -1;
while(n > 0){
n /= 10;
cnt++;
}
n = num / (int) Math.pow(10, cnt);
n = n * (int) Math.pow(10, cnt);
System.out.println(n);
System.out.println(cnt);
if(cnt == 3){
sb.append("M".repeat(n / 1000));
}
if(cnt == 2){
if(n == 900){
sb.append("CM");
}else if(n == 400){
sb.append("CD");
}else if(n < 500){
sb.append("C".repeat(n / 100));
}else{
sb.append("D");
sb.append("C".repeat((n-500) / 100));
}
}
if(cnt == 1){
if(n == 90){
sb.append("XC");
}else if(n == 40){
sb.append("XL");
}else if(n < 50){
sb.append("X".repeat(n / 10));
}else{
sb.append("L");
sb.append("X".repeat((n-50) / 10));
}
}
if(cnt == 0){
if(n == 9){
sb.append("IX");
}else if(n == 4){
sb.append("IV");
}else if(n < 5){
sb.append("I".repeat(n));
}else{
sb.append("V");
sb.append("I".repeat((n-5)));
}
}
System.out.println(num);
System.out.println(n);
num = num - n;
}
return sb.toString();
}
}