-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode13.java
More file actions
58 lines (58 loc) · 922 Bytes
/
Copy pathleetcode13.java
File metadata and controls
58 lines (58 loc) · 922 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution {
public int romanToInt(String s) {
int prev = 1000;
int result = 0;
for (char c : s.toCharArray()) {
switch (c) {
case 'I':
result += 1;
prev = 1;
break;
case 'V':
if (5 > prev) {
result -= (2 * prev);
}
result += 5;
prev = 5;
break;
case 'X':
if (10 > prev) {
result -= (2 * prev);
}
result += 10;
prev = 10;
break;
case 'L':
if (50 > prev) {
result -= (2 * prev);
}
result += 50;
prev = 50;
break;
case 'C':
if (100 > prev) {
result -= (2 * prev);
}
result += 100;
prev = 100;
break;
case 'D':
if (500 > prev) {
result -= (2 * prev);
}
result += 500;
prev = 500;
break;
case 'M':
if (1000 > prev) {
result -= (2 * prev);
}
result += 1000;
prev = 1000;
break;
default: break;
}
}
return result;
}
}