-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomanToInt.java
More file actions
40 lines (35 loc) · 1013 Bytes
/
RomanToInt.java
File metadata and controls
40 lines (35 loc) · 1013 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
30
31
32
33
34
35
36
37
38
39
40
import java.util.HashMap;
import java.util.Map;
public class RomanToInt {
public int romanToInt(String s) {
Map<String, Integer> rToA = new HashMap<>();
rToA.put("I", 1);
rToA.put("IV", 4);
rToA.put("V", 5);
rToA.put("IX", 9);
rToA.put("X", 10);
rToA.put("XL", 40);
rToA.put("L", 50);
rToA.put("XC", 90);
rToA.put("C", 100);
rToA.put("CD", 400);
rToA.put("D", 500);
rToA.put("CM", 900);
rToA.put("M", 1000);
int sol = 0;
int sLength = s.length();
for (int i = 0; i < sLength; i++) {
String r = s.substring(i, i + 1);
var a = rToA.get(r);
if (i + 1 != sLength) {
String both = r + String.valueOf(s.charAt(i + 1));
if (rToA.get(both) != null) {
a = rToA.get(both);
i++;
}
}
sol += a;
}
return sol;
}
}