Skip to content

Commit e40ab22

Browse files
update 13
1 parent e66e6ba commit e40ab22

File tree

2 files changed

+16
-0
lines changed
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

2 files changed

+16
-0
lines changed

Diff for: src/main/java/com/fishercoder/solutions/firstthousand/_13.java

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
public class _13 {
77

88
public static class Solution1 {
9+
/**
10+
* This is the most concise/elegant version to solve this problem:
11+
* 1. use a map to hold the roman to numeric mappings;
12+
* 2. we always add the number (corresponding from the char) first, then once we find that a later char has a corresponding value that is bigger than the previous char,
13+
* we deduct 2 * previousCharCorrespondingValue
14+
* <p>
15+
* e.g. XIV step by step is computed below:
16+
* 0 + 10 = 10
17+
* 10 + 1 = 11
18+
* 11 + (5 - 2*1) = 14
19+
*/
920
public int romanToInt(String s) {
1021
Map<Character, Integer> map = new HashMap();
1122
map.put('I', 1);

Diff for: src/test/java/com/fishercoder/firstthousand/_13Test.java

+5
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ public void test2() {
2828
public void test3() {
2929
assertEquals(3999, solution1.romanToInt("MMMCMXCIX"));
3030
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(3045, solution1.romanToInt("MMMXLV"));
35+
}
3136
}

0 commit comments

Comments
 (0)