-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
Copy pathIntegertoRoman.java
47 lines (44 loc) · 1.11 KB
/
IntegertoRoman.java
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
package problems.medium;
/**
* Created by sherxon on 2016-12-24.
*/
public class IntegertoRoman {
public String intToRoman(int num) {
int i=1;
StringBuilder sb= new StringBuilder();
while(num!=0){
int rem=num % 10;
sb.insert(0, get(i, rem));
i*=10;
num/=10;
}
return sb.toString();
}
public String get(int i, int val){
String s="";
if(i==1){
s+=get2("I", "V", "X", val);
}else if (i==10){
s+=get2("X", "L", "C", val);
}else if(i==100){
s+=get2("C", "D", "M", val);
}else{
s+=get2("M", "", "", val);
}
return s;
}
public String get2(String l, String h, String hh, int val){
switch(val){
case 1:return l;
case 2:return l+l;
case 3:return l+l+l;
case 4:return l+h;
case 5:return h;
case 6:return h+l;
case 7:return h+l+l;
case 8:return h+l+l+l;
case 9:return l+hh;
}
return "";
}
}