-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass.java
More file actions
51 lines (43 loc) · 1.43 KB
/
MainClass.java
File metadata and controls
51 lines (43 loc) · 1.43 KB
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
42
43
44
45
46
47
48
49
50
51
import java.io.*;
import javax.json.JsonArray;
class Solution {
public int romanToInt(String s) {
HashMap<Character, Integer> romanMap = new HashMap<Character, Integer>();
int output = 0;
int previous = 0;
int current = 0;
romanMap.put('I', 1);
romanMap.put('V', 5);
romanMap.put('X', 10);
romanMap.put('L', 50);
romanMap.put('C', 100);
romanMap.put('D', 500);
romanMap.put('M', 1000);
for(int i=(s.length()-1); i>=0; i--){
current = romanMap.get(s.charAt(i));
if (previous != 0 && previous > current) {
current *= -1;
previous = 0;
} else {
previous = current;
}
output += current;
}
return output;
}
}
public class MainClass {
public static String stringToString(String input) {
return JsonArray.readFrom("[" + input + "]").get(0).asString();
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
String s = stringToString(line);
int ret = new Solution().romanToInt(s);
String out = String.valueOf(ret);
System.out.print(out);
}
}
}