Contents
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
/*
* Given a roman numeral, convert it to an integer.

* Input is guaranteed to be within the range from 1 to 3999.
* I = 1;
* V = 5;
* X = 10;
* L = 50;
* C = 100;
* D = 500;
* M = 1000;
*
* the numeral I can be placed before V and X to make 4 units (IV) and 9 units
* (IX respectively)
* X can be placed before L and C to make 40 (XL) and 90 (XC respectively)
* C can be placed before D and M to make 400 (CD) and 900 (CM) according to
* the same pattern
*/


public class RomanToInteger {

public int romanToInt(String s) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
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);
//因为下面的for循环里要判断i和i+1的情况,所以这里初始化的时候处理一下,不然就非法访问了
int result = map.get(s.charAt(s.length() - 1));
for (int i = s.length() - 2; i >= 0; i--) {
result += (map.get(s.charAt(i)) < map.get(s.charAt(i + 1)) ? //按罗马数字的规则模拟
-1 : 1) * map.get(s.charAt(i));
}
return result;
}
}
Contents