-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12.integer-to-roman.java
More file actions
81 lines (60 loc) · 1.75 KB
/
Copy path12.integer-to-roman.java
File metadata and controls
81 lines (60 loc) · 1.75 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
71
72
73
74
75
76
77
78
79
80
81
class Solution {
public String intToRoman(int num) {
HashMap<Integer, String> hsm = new HashMap<>();
hsm.put(1, "I");
hsm.put(4, "IV");
hsm.put(5, "V");
hsm.put(9, "IX");
hsm.put(10, "X");
hsm.put(40, "XL");
hsm.put(50, "L");
hsm.put(90, "XC");
hsm.put(100, "C");
hsm.put(400, "CD");
hsm.put(500, "D");
hsm.put(900, "CM");
hsm.put(1000, "M");
StringBuilder res = new StringBuilder();
while(num > 0){
if(hsm.containsKey(num))
{
res.append(hsm.get(num));
num = 0;
}
else{
int p = getnearnum(num );
// System.out.println(p);
res.append(hsm.get(p));
num = num-p;
}
}
return res.toString();
}
public int getnearnum(int num){
if(num >= 1 && num <=3)
return 1;
if(num >= 4 && num < 5)
return 4;
if(num >= 5 && num < 9)
return 5;
if(num == 9)
return 9;
if(num >= 10 && num < 40)
return 10;
if(num >= 40 && num <= 49)
return 40;
if(num > 49 && num < 90)
return 50;
if(num >= 90 && num< 100)
return 90;
if(num >= 100 && num < 400)
return 100;
if(num >= 400 && num < 500)
return 400;
if(num >= 500 && num < 900)
return 500;
if(num >= 900 && num < 1000)
return 900;
return 1000;
}
}