-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
46 lines (40 loc) · 1.09 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.HashMap;
import java.util.Map;
/**
* @author: changle
* @time: 2019-06-13 02:33
* source: https://leetcode.com/problems/roman-to-integer/
*/
public class Solution {
Map<String, Integer> map = new HashMap<>();
//map 初始化
public void initMap() {
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);
}
public int romanToInt(String s) {
initMap();
int result = 0;
for (int i = 0; i < s.length(); i++) {
//判断是否是两位字符表示的数字
if (s.length() - i > 1 && map.get(s.substring(i, i + 2)) != null) {
result += map.get(s.substring(i, i + 2));
i++;
} else {
result += map.get("" + s.charAt(i));
}
}
return result;
}
}