-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
34 lines (28 loc) · 866 Bytes
/
solution.java
File metadata and controls
34 lines (28 loc) · 866 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
import java.util.HashMap;
class Solution {
public int romanToInt(String s) {
// Roman numeral symbol to integer conversion map
HashMap<Character, Integer> conv = new HashMap<>();
conv.put('I', 1);
conv.put('V', 5);
conv.put('X', 10);
conv.put('L', 50);
conv.put('C', 100);
conv.put('D', 500);
conv.put('M', 1000);
int res = 0;
int len = s.length();
for(int i = 0; i < len; i++){
int curr = conv.get(s.charAt(i));
if(i != (len - 1)){
int next = conv.get(s.charAt(i+1));
if(curr < next){
curr = (next - curr);
i++; // Skip next numeral
}
}
res += curr;
}
return res;
}
}