-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13. Roman to Integer
71 lines (61 loc) · 1.6 KB
/
13. Roman to Integer
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Runtime 2 ms
Beats 100%
Memory 43.6 MB
Beats 64.95%
class Solution {
public int romanToInt(String s) {
int res=0;
char st[]=s.toCharArray();
int n=st.length;
for(int i=0;i<n;i++){
if(st[i] == 'I'){
res+=1;
if(i+1 < n){
if(st[i+1] == 'V'){
res+=3;
i++;
}
else if(st[i+1] == 'X'){
res+=8;
i++;
}
}
}
else if(st[i] == 'V')
res+=5;
else if(st[i] == 'X'){
res+=10;
if(i+1<n){
if(st[i+1] == 'L'){
res+=30;
i++;
}
else if(st[i+1] == 'C'){
res+=80;
i++;
}
}
}
else if(st[i] == 'L')
res+=50;
else if(st[i] == 'C'){
res+=100;
if(i+1 < n){
if(st[i+1] == 'D'){
res+=300;
i++;
}
else if(st[i+1] == 'M'){
res+=800;
i++;
}
}
}
else if(st[i] == 'D')
res+=500;
else if(st[i] == 'M')
res+=1000;
}
return res;
}
}