-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
44 lines (37 loc) · 1.08 KB
/
Solution.java
File metadata and controls
44 lines (37 loc) · 1.08 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
package leetCode_13;
import java.util.HashMap;
/**
* @author dimdark
* @date 2017-10-06
* @time 4:53 PM
*/
public class Solution {
private static HashMap<Character, Integer> romanMap = new HashMap<Character, Integer>();
private static final String ROMAN_STRING = "IVXLCDM";
static {
romanMap.put('I', 1);
romanMap.put('V', 5);
romanMap.put('X', 10);
romanMap.put('L', 50);
romanMap.put('C', 100);
romanMap.put('D', 500);
romanMap.put('M', 1000);
}
public int romanToInt(String s) {
if (s == null || s.length() == 0) return 0;
char[] cs = s.toCharArray();
int rst = romanMap.get(cs[cs.length - 1]);
int i = cs.length - 2, j = cs.length - 1;
while (i >= 0) { // O(n)
int idx1 = ROMAN_STRING.indexOf(cs[i]);
int idx2 = ROMAN_STRING.indexOf(cs[j]);
if (idx1 >= idx2) {
rst += romanMap.get(cs[i]);
} else {
rst -= romanMap.get(cs[i]);
}
--i; --j;
}
return rst;
}
}