-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomToInt.java
More file actions
34 lines (28 loc) · 853 Bytes
/
RomToInt.java
File metadata and controls
34 lines (28 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
import java.util.HashMap;
public class RomToInt {
public static void main(String args[]){
HashMap<String, Integer> map= new HashMap<>();
map.put("I",1);
map.put("V",5);
map.put("X",10);
map.put("L",50);
map.put("C",100);
map.put("D",500);
map.put("M",1000);
String s = "MCMXCIV";
int len = s.length();
int num=0;
for(int i =len-1; i>=0; i--){
if(i==(len-1)){
num = num + map.get(""+s.charAt(i));
}
else if(map.get(""+s.charAt(i))<map.get(""+s.charAt(i+1))){
num = num - map.get(""+s.charAt(i));
}
else if(map.get(""+s.charAt(i))>=map.get(""+s.charAt(i+1))){
num = num + map.get(""+s.charAt(i));
}
}
System.out.print(num);
}
}