Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit edeba98

Browse files
LeetCode problem: 13. Roman to Integer
1 parent e1ff9ed commit edeba98

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 13. Roman to Integer
2+
3+
Difficulty: `Easy`
4+
Topics: `Hash Table`, `Math`, `String`
5+
6+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
7+
8+
```
9+
Symbol Value
10+
I 1
11+
V 5
12+
X 10
13+
L 50
14+
C 100
15+
D 500
16+
M 1000
17+
```
18+
19+
For example, `2` is written as `II` in Roman numeral, just two ones added together. `12` is written as `XII`, which is
20+
simply `X + II`. The number `27` is written as `XXVII`, which is `XX + V + II`.
21+
22+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`.
23+
Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same
24+
principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used:
25+
26+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
27+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
28+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
29+
30+
Given a roman numeral, convert it to an integer.
31+
32+
**Example 1:**
33+
34+
````text
35+
Input: s = "III"
36+
Output: 3
37+
Explanation: III = 3.
38+
Example 2:
39+
````
40+
41+
**Example 2:**
42+
43+
```text
44+
Input: s = "LVIII"
45+
Output: 58
46+
Explanation: L = 50, V= 5, III = 3.
47+
Example 3:
48+
```
49+
50+
**Example 3:**
51+
52+
```text
53+
Input: s = "MCMXCIV"
54+
Output: 1994
55+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
56+
```
57+
58+
**Constraints:**
59+
60+
* `1 <= s.length <= 15`
61+
* `s` contains only the characters (`I`, `V`, `X`, `L`, `C`, `D`, `M`).
62+
* It is guaranteed that `s` is a valid roman numeral in the range `[1, 3999]`.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.bl.roman_to_integer;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* This is the solution to the LeetCode problem: 13. Roman to Integer
8+
*
9+
* @author Børre A. Opedal Lunde
10+
* @since 2024年01月23日
11+
*/
12+
class Solution {
13+
14+
// A map of roman numeral symbols by value.
15+
static final Map<Character, Integer> ROMAN_NUMERAL_SYMBOLS_BY_VALUE = new HashMap<>();
16+
17+
// Initialize the map.
18+
static {
19+
ROMAN_NUMERAL_SYMBOLS_BY_VALUE.put('I', 1);
20+
ROMAN_NUMERAL_SYMBOLS_BY_VALUE.put('V', 5);
21+
ROMAN_NUMERAL_SYMBOLS_BY_VALUE.put('X', 10);
22+
ROMAN_NUMERAL_SYMBOLS_BY_VALUE.put('L', 50);
23+
ROMAN_NUMERAL_SYMBOLS_BY_VALUE.put('C', 100);
24+
ROMAN_NUMERAL_SYMBOLS_BY_VALUE.put('D', 500);
25+
ROMAN_NUMERAL_SYMBOLS_BY_VALUE.put('M', 1000);
26+
}
27+
28+
public int romanToInt(String romanNumberAsString) {
29+
30+
// The sum of the roman numeral symbols.
31+
int sum = 0;
32+
33+
// Convert the roman numeral string to a character array that we can
34+
// iterate over.
35+
final char[] characters = romanNumberAsString.toCharArray();
36+
37+
// The value of the current roman numeral symbol (used in the loop).
38+
int currentValue;
39+
40+
// The value of the previous roman numeral symbol (used in the loop).
41+
int previousValue = 0;
42+
43+
// Iterate over the roman numeral symbols in reverse order. That way we
44+
// can traverse the roman numeral symbols in "ascending" order.
45+
for (int i = characters.length - 1; i >= 0; i--) {
46+
47+
// This is the current character we're iterating over.
48+
final char character = characters[i];
49+
50+
// Retrieve the value of the roman numeral symbol.
51+
currentValue = ROMAN_NUMERAL_SYMBOLS_BY_VALUE.get(character);
52+
53+
// If the previous value is greater than the current value, we need
54+
// to subtract the current value from the sum. Otherwise, we need to
55+
// add the current value to the sum.
56+
if (previousValue > currentValue) {
57+
sum -= currentValue;
58+
} else {
59+
sum += currentValue;
60+
}
61+
62+
// Set the previous value to the current value.
63+
previousValue = currentValue;
64+
}
65+
66+
// Return the sum.
67+
return sum;
68+
}
69+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.bl.roman_to_integer;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
/**
9+
* This is the test to the LeetCode problem: 13. Roman to Integer
10+
*
11+
* @author Børre A. Opedal Lunde
12+
* @since 2024年01月23日
13+
*/
14+
@DisplayName("Roman to Integer")
15+
class SolutionTest {
16+
17+
@Test
18+
@DisplayName("Example one")
19+
void exampleOne() {
20+
String s = "III";
21+
int expected = 3;
22+
assertEquals(expected, new Solution().romanToInt(s));
23+
}
24+
25+
@Test
26+
@DisplayName("Example two")
27+
void exampleTwo() {
28+
String s = "LVIII";
29+
int expected = 58;
30+
assertEquals(expected, new Solution().romanToInt(s));
31+
}
32+
33+
@Test
34+
@DisplayName("Example three")
35+
void exampleThree() {
36+
String s = "MCMXCIV";
37+
int expected = 1994;
38+
assertEquals(expected, new Solution().romanToInt(s));
39+
}
40+
41+
@Test
42+
@DisplayName("Roman IV")
43+
void romanIv() {
44+
String s = "IV";
45+
int expected = 4;
46+
assertEquals(expected, new Solution().romanToInt(s));
47+
}
48+
49+
@Test
50+
@DisplayName("Roman VII")
51+
void romanVii() {
52+
String s = "VII";
53+
int expected = 7;
54+
assertEquals(expected, new Solution().romanToInt(s));
55+
}
56+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /