-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13-RomanToInteger.java
More file actions
36 lines (35 loc) · 861 Bytes
/
Copy path13-RomanToInteger.java
File metadata and controls
36 lines (35 loc) · 861 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
class Solution {
public int romanToInt(String s) {
int res = 0;
for (int i = 0; i < s.length() - 1; i++) {
int curr = charToInt(s.charAt(i));
int next = charToInt(s.charAt(i + 1));
if (curr < next) {
res -= curr;
} else {
res += curr;
}
}
res += charToInt(s.charAt(s.length() - 1));
return res;
}
private int charToInt(char c) {
int res = 0;
if (c == 'I')
res = 1;
else if (c == 'V')
res = 5;
else if (c == 'X')
res = 10;
else if (c == 'L')
res = 50;
else if (c == 'C')
res = 100;
else if (c == 'D')
res = 500;
else {
res = 1000;
}
return res;
}
}