-
Notifications
You must be signed in to change notification settings - Fork 0
/
13.txt
75 lines (75 loc) · 2.26 KB
/
13.txt
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
72
73
74
75
class Solution {
public int romanToInt(String s) {
int arabic = 0;
char[] roman = s.toCharArray();
for (int i = 0; i <= roman.length - 1; i++) {
if (roman[i] == 'V') {
arabic += 5;
}
else if (roman[i] == 'L') {
arabic += 50;
}
else if (roman[i] == 'D') {
arabic += 500;
}
else if (roman[i] == 'M') {
arabic += 1000;
}
else if (roman[i] == 'I') {
if(i != roman.length - 1) {
if (roman[i] == 'I' && roman[i+1] == 'V') {
arabic += 4;
i++;
}
else if (roman[i] == 'I' && roman[i+1] == 'X') {
arabic +=9;
i++;
}
else {
arabic += 1;
}
}
else {
arabic += 1;
}
}
else if (roman[i] == 'X') {
if(i != roman.length - 1) {
if (roman[i] == 'X' && roman[i+1] == 'L') {
arabic += 40;
i++;
}
else if (roman[i] == 'X' && roman[i+1] == 'C') {
arabic +=90;
i++;
}
else {
arabic += 10;
}
}
else {
arabic += 10;
}
}
else if (roman[i] == 'C') {
if(i != roman.length - 1) {
if (roman[i] == 'C' && roman[i+1] == 'D') {
arabic += 400;
i++;
}
else if (roman[i] == 'C' && roman[i+1] == 'M') {
arabic +=900;
i++;
}
else {
arabic += 100;
}
}
else {
arabic += 100;
}
}
}
return arabic;
}
}