-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomanToInteger.java
More file actions
32 lines (30 loc) · 815 Bytes
/
RomanToInteger.java
File metadata and controls
32 lines (30 loc) · 815 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
public class Solution {
static final int[] table = new int[128];
static {
table['I'] = 1;
table['V'] = 5;
table['X'] = 10;
table['L'] = 50;
table['C'] = 100;
table['D'] = 500;
table['M'] = 1000;
}
public int romanToInt(String s) {
int result = 0;
int pos = 0;
while (pos < s.length()) {
char pchar = s.charAt(pos);
if (pos < s.length() - 1) {
char nchar = s.charAt(pos + 1);
if (table[pchar] < table[nchar]) {
result += table[nchar] - table[pchar];
pos += 2;
continue;
}
}
result += table[pchar];
pos++;
}
return result;
}
}