-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomanInteger.java
More file actions
52 lines (42 loc) · 1.48 KB
/
RomanInteger.java
File metadata and controls
52 lines (42 loc) · 1.48 KB
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
44
45
46
47
48
49
50
51
import java.util.*;
import java.util.Map;
public class RomanInteger {
private static final Map<Character, Integer> VAL = new HashMap<>();
static {
VAL.put('I', 1);
VAL.put('V', 5);
VAL.put('X', 10);
VAL.put('L', 50);
VAL.put('C', 100);
VAL.put('D', 500);
VAL.put('M', 1000);
}
// Convert Roman numeral to integer. Throws IllegalArgumentException for invalid input.
public int romanToInt(String s) {
if (s == null || s.isEmpty()) throw new IllegalArgumentException("Input is null/empty");
s = s.toUpperCase(Locale.ROOT);
int total = 0;
int prev = 0;
for (int i = s.length() - 1; i >= 0; i--) {
char ch = s.charAt(i);
Integer val = VAL.get(ch);
if (val == null) throw new IllegalArgumentException("Invalid Roman numeral character: " + ch);
if (val < prev) total -= val;
else total += val;
prev = val;
}
return total;
}
// quick tests
public static void main(String[] args) {
RomanInteger r = new RomanInteger();
String[] tests = {"III", "IV", "IX", "LVIII", "MCMXCIV", "mmxxv", "invalid"};
for (String t : tests) {
try {
System.out.printf("%s -> %d%n", t, r.romanToInt(t));
} catch (IllegalArgumentException ex) {
System.out.printf("%s -> ERROR: %s%n", t, ex.getMessage());
}
}
}
}