forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_13.java
31 lines (26 loc) · 850 Bytes
/
_13.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
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
public class _13 {
public static class Solution1 {
public int romanToInt(String s) {
Map<Character, 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);
int result = 0;
for (int i = 0; i < s.length(); i++) {
if (i > 0 && map.get(s.charAt(i)) > map.get(s.charAt(i - 1))) {
result += map.get(s.charAt(i)) - 2 * map.get(s.charAt(i - 1));
} else {
result += map.get(s.charAt(i));
}
}
return result;
}
}
}