📘
Roman to Integer
EasyMath & Design
Convert a Roman numeral string to an integer.
I=1, V=5, X=10, L=50, C=100, D=500, M=1000. Subtractive: IV=4, IX=9, XL=40, XC=90, CD=400, CM=900.
Example:
Input:
III
Output:
3
Reasoning:
- The input string
IIIis analyzed from left to right to determine the integer equivalent of each Roman numeral. - Since each
Ihas a value of 1 and there are no subtractive cases in the input, we simply add the values of the threeIs together: 1+1+1=3. - The final output is the sum calculated in the previous step, which is 3.
Constraints:
- 1 <= len(s) <= 15
- s is a valid Roman numeral in [1, 3999]
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.