-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
executable file
·37 lines (32 loc) · 1003 Bytes
/
Solution.java
File metadata and controls
executable file
·37 lines (32 loc) · 1003 Bytes
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
package $013;
import java.util.HashMap;
/**
* @author Junlan Shuai[shuaijunlan@gmail.com].
* @date Created on 15:56 2017/3/15.
*/
public class Solution {
public int romanToInt(String s) {
HashMap<Character, Integer> hashMap = new HashMap<>();
hashMap.put('I', 1);
hashMap.put('V', 5);
hashMap.put('X', 10);
hashMap.put('L', 50);
hashMap.put('C', 100);
hashMap.put('D', 500);
hashMap.put('M', 1000);
char[] str = s.toCharArray();
int sum = 0, length = str.length - 1;
for (int i = 0; i < length; i++) {
if (hashMap.get(str[i + 1]) <= hashMap.get(str[i]))
sum += hashMap.get(str[i]);
else
sum -= hashMap.get(str[i]);
}
sum += hashMap.get(str[length]);
return sum;
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.romanToInt("DCXXI"));
}
}