-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRomanToInt.java
43 lines (32 loc) · 896 Bytes
/
RomanToInt.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
42
43
import java.util.HashMap;
public class RomanToInt {
public int romanToInt(String s) {
HashMap<Character,Integer> map=new HashMap<>();
map.put('I',1);
map.put('V',5);
map.put('X',10);
map.put('L',50);
map.put('D',500);
map.put('C',100);
map.put('M',1000);
int res=0;
for (int i = 0; i <s.length(); i++) {
int v1=map.get(s.charAt(i));
int v2=(i+1)<s.length()?map.get(s.charAt(i+1)):-1;
if(v2>v1){
res+=v2-v1;
i++;
}
else
res+=v1;
}
return res;
}
public static void main(String[] args) {
//String s="III";
String s="LVIII";
RomanToInt romanToInt=new RomanToInt();
int res= romanToInt.romanToInt(s);
System.out.println(res);
}
}