-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
31 lines (28 loc) · 843 Bytes
/
Main.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
package com.company;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// write your code here
}
public int romanToInt(String s) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>(){{
put('I', 1);
put('V', 5);
put('X', 10);
put('L', 50);
put('C', 100);
put('D', 500);
put('M', 1000);
}};
int[] digits = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
digits[i] = map.get(s.charAt(i));
if(i != 0 && map.get(s.charAt(i - 1)) < map.get(s.charAt(i)))
digits[i - 1] *= -1;
}
int res = 0;
for (int i = 0; i < digits.length; i++)
res += digits[i];
return res;
}
}