forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomanToInteger.java
More file actions
24 lines (19 loc) · 626 Bytes
/
RomanToInteger.java
File metadata and controls
24 lines (19 loc) · 626 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
public class RomanToInteger {
public int romanToInt(String s) {
int len = s.length();
int n = 0;
for (int i = len - 1; i >= 0; i--) {
char c = s.charAt(i);
switch (c) {
case 'V': n += 5; break;
case 'L': n += 50; break;
case 'D': n += 500; break;
case 'I': n += (n >= 5 ? -1 : 1); break;
case 'X': n += (n >= 50 ? -10 : 10); break;
case 'C': n += (n >= 500 ? -100 : 100); break;
case 'M': n += 1000; break;
}
}
return n;
}
}