-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.java
41 lines (38 loc) · 1.04 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
package _013;
import java.util.HashMap;
import java.util.Map;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/04/25
* desc :
* </pre>
*/
public class Solution {
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 len = s.length();
int sum = map.get(s.charAt(len - 1));
for (int i = len - 2; i >= 0; --i) {
if (map.get(s.charAt(i)) < map.get(s.charAt(i + 1))) {
sum -= map.get(s.charAt(i));
} else {
sum += map.get(s.charAt(i));
}
}
return sum;
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.romanToInt("DCXXI"));// 621
System.out.println(solution.romanToInt("CCCXLVIII"));// 348
}
}