-
Notifications
You must be signed in to change notification settings - Fork 1
/
No13罗马数字转整数.java
43 lines (34 loc) · 1.42 KB
/
No13罗马数字转整数.java
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
package leetCode.repository;
public class No13罗马数字转整数 {
public static void main(String[] args) {
System.out.println(new No13罗马数字转整数().romanToInt("III") == 3);
System.out.println(new No13罗马数字转整数().romanToInt("IV") == 4);
System.out.println(new No13罗马数字转整数().romanToInt("IX") == 9);
System.out.println(new No13罗马数字转整数().romanToInt("MCMXCIV") == 1994);
}
public int romanToInt(String s) {
return new Solution1().romanToInt(s);
}
static class Solution1 implements RomanToInt {
private static int[] table = new int[]{1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
private static String[] roma = new String[]{"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
@Override
public int romanToInt(String s) {
int sum = 0;
while (s.length() > 0) {
for (int i = roma.length - 1; i >= 0; i--) {
if (s.startsWith(roma[i])) {
s = s.substring(roma[i].length());
sum += table[i];
// System.out.printf("talbe[%d]=%d,s=\"%s\"\n", i, table[i], s);
break;
}
}
}
return sum;
}
}
interface RomanToInt {
int romanToInt(String s);
}
}