-
Notifications
You must be signed in to change notification settings - Fork 0
/
13. Roman to Integer.java
46 lines (46 loc) · 1.37 KB
/
13. Roman to Integer.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
44
45
46
class Solution {
public int romanToInt(String s) {
int res = 0, prev = 0, curr = 0;
for(int i = (s.length()-1); i >= 0; i--){
switch(s.charAt(i)){
case 'I':
curr = 1;
break;
case 'V':
curr = 5;
break;
case 'X':
curr = 10;
break;
case 'L':
curr = 50;
break;
case 'C':
curr = 100;
break;
case 'D':
curr = 500;
break;
case 'M':
curr = 1000;
break;
}
if(curr < prev){
res -= curr;
prev = curr;
}
else{
res += curr;
prev = curr;
}
}
return res;
}
}