-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#0013 Roman to Integer.py
40 lines (33 loc) · 1.21 KB
/
#0013 Roman to Integer.py
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
class Solution:
def romanToInt(self, s: str) -> int:
"""
Convert a Roman numeral string to an integer.
Roman numerals are represented by the following characters:
- I: 1
- V: 5
- X: 10
- L: 50
- C: 100
- D: 500
- M: 1000
Args:
s (str): A Roman numeral string.
Returns:
int: The integer representation of the Roman numeral.
"""
# Dictionary to map Roman numerals to their integer values
convert = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
total = 0
# Iterate over the string except the last character
for i in range(len(s) - 1):
current_value = convert[s[i]]
next_value = convert[s[i + 1]]
# If the current value is less than the next value, subtract it from the total
if current_value < next_value:
total -= current_value
else:
# Otherwise, add it to the total
total += current_value
# Add the value of the last character to the total
total += convert[s[-1]]
return total