-
Notifications
You must be signed in to change notification settings - Fork 1
/
13thJune(VI).java
41 lines (37 loc) · 1.19 KB
/
13thJune(VI).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
32
33
34
35
36
37
38
39
40
41
import java.util.HashMap;
public class rough {
static int romanToInt(String s){
// store the keys in hashmap -- hardcoded
// in reverse order ... M ,D, C, L... I
//use a loop ... extract current symbol find it's current value using the hashmap
// compare the value with next character
// if (i<i+1) then sum-=current value : else = sum+=current value
HashMap<Character, Integer> hm = new HashMap<>();
hm.put('M',1000);
hm.put('D',500);
hm.put('C',100);
hm.put('L',50);
hm.put('X',10);
hm.put('V',5);
hm.put('I', 1);
char[] chars = s.toCharArray();
// int sum = hm.get(chars[0]);
int sum = 0;
for(int i = 1; i<chars.length;i++){
if(hm.get(chars[i-1])<hm.get(chars[i])){
sum-=hm.get(chars[i-1]);
}
else{
sum+=hm.get(chars[i-1]);
}
}
return sum+hm.get(chars[s.length()-1]);
// System.out.println(hm);
}
public static void main(String[] args) {
// String s = "LVIII";
String s = "V";
System.out.println(romanToInt(s));
// romanToInt(s);
}
}