-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomanToInt.java
More file actions
34 lines (25 loc) · 853 Bytes
/
RomanToInt.java
File metadata and controls
34 lines (25 loc) · 853 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
class Solution {
public int romanToInt(String str) {
if(str == null || str.length() == 0) {
return 0;
}
Map<Character, Integer> charMap = new HashMap<>();
charMap.put('I', 1);
charMap.put('V', 5);
charMap.put('X', 10);
charMap.put('L', 50);
charMap.put('C', 100);
charMap.put('D', 500);
charMap.put('M', 1000);
int result = 0;
for(int i = 0; i < str.length()-1; i++) {
if(charMap.get(str.charAt(i)) >= charMap.get(str.charAt(i+1))) {
result = result + charMap.get(str.charAt(i));
} else {
result = result - charMap.get(str.charAt(i));
}
}
result = result + charMap.get(str.charAt(str.length()-1));
return result;
}
}