forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
34 lines (32 loc) · 933 Bytes
/
Solution.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
class Solution {
public int romanToInt(String s) {
Map<String, Integer> map = new HashMap<String, Integer>(13) {
{
put("I", 1);
put("V", 5);
put("X", 10);
put("L", 50);
put("C", 100);
put("D", 500);
put("M", 1000);
put("IV", 4);
put("IX", 9);
put("XL", 40);
put("XC", 90);
put("CD", 400);
put("CM", 900);
}
};
int res = 0;
int len = s.length();
for (int i = 0; i < len; ++i) {
if (i != len - 1 && map.get(s.substring(i, i + 2)) != null) {
res += map.get(s.substring(i, i + 2));
++i;
continue;
}
res += map.get(s.substring(i, i + 1));
}
return res;
}
}