-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy path13RomanToInteger.java
More file actions
39 lines (33 loc) · 980 Bytes
/
13RomanToInteger.java
File metadata and controls
39 lines (33 loc) · 980 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
class Solution {
public int romanToInt(String s) {
Map<String, Integer> map = new HashMap();
map.put("I", 1);
map.put("V", 5);
map.put("X", 10);
map.put("L", 50);
map.put("C", 100);
map.put("D", 500);
map.put("M", 1000);
map.put("IV", 4);
map.put("IX", 9);
map.put("XL", 40);
map.put("XC", 90);
map.put("CD", 400);
map.put("CM", 900);
int i = 0,
sum = 0;
while (i < s.length()) {
if (i < s.length() - 1) {
String digit = s.substring(i, i + 2);
if (map.containsKey(digit)) {
sum += map.get(digit);
i += 2;
continue;
}
}
sum += map.get(Character.toString(s.charAt(i)));
i += 1;
}
return sum;
}
}