-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_13.java
More file actions
26 lines (24 loc) · 687 Bytes
/
Copy pathP_13.java
File metadata and controls
26 lines (24 loc) · 687 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
package leetcode.easy;
public class P_13 {
public int romanToInt(String s) {
final int n = s.length();
final char[] w = s.toCharArray();
final int[] map = new int[26];
map['I' - 'A'] = 1;
map['V' - 'A'] = 5;
map['X' - 'A'] = 10;
map['L' - 'A'] = 50;
map['C' - 'A'] = 100;
map['D' - 'A'] = 500;
map['M' - 'A'] = 1000;
int res = 0;
for (int i = 0; i < n; i++) {
if (i == n - 1 || map[w[i + 1] - 'A'] <= map[w[i] - 'A']) {
res += map[w[i] - 'A'];
} else {
res -= map[w[i] - 'A'];
}
}
return res;
}
}