forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegertoroman.java
More file actions
executable file
·36 lines (35 loc) · 1.15 KB
/
integertoroman.java
File metadata and controls
executable file
·36 lines (35 loc) · 1.15 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
public class Solution {
public String intToRoman(int num) {
// Start typing your Java solution below
// DO NOT write main() function
StringBuilder ans = new StringBuilder("");
char a[][] ={ {'I','V','X'}, {'X','L','C'}, {'C','D','M'}, {'M','M','M'}};
int threshold = 1000;
for(int i=3;i>=0;i--){
if(num>=threshold){
int k = num/threshold;
num -= k*threshold;
if(k==0){
}
else if(k==9){
ans.append(a[i][0]);
ans.append(a[i][2]);
}else if(k<=3){
for(int j=0;j<k;j++){
ans.append(a[i][0]);
}
}else if(k==4){
ans.append(a[i][0]);
ans.append(a[i][1]);
}else if(k<=8){
ans.append(a[i][1]);
for(int j=0;j<k-5;j++){
ans.append(a[i][0]);
}
}
}
threshold /=10;
}
return ans.toString();
}
}