forked from Google-DSC-TMSL/ProjectAlgorithms
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegertoroman.java
More file actions
Latest commit
29 lines (27 loc) · 684 Bytes
/
Copy pathintegertoroman.java
File metadata and controls
29 lines (27 loc) · 684 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
29
// M - 1000
// CM - 900
// D - 500
// CD - 400
// C - 100
// XC - 90
// L - 50
// XL - 40
// X - 10
// IX - 9
// V - 5
// IV - 4
// I - 1
classSolution {
publicStringintToRoman(intnum) {
int[] intCode = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
String[] code = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
StringBuildersb = newStringBuilder();
for(inti = 0; i < intCode.length; i++){
while(num >= intCode[i]){
sb.append(code[i]);
num-=intCode[i];
}
}
returnsb.toString();
}
}