Roman to Integer
Roman to Integer
Roman numerals are represented by seven symbols: I (1), V (5), X (10), L (50), C (100), D (500), and M (1000).
Typically, numerals are written from largest to smallest, and their values are summed. For instance, XII is 10 + 1 + 1 = 12, and LVIII is 50 + 5 + 1 + 1 + 1 = 58.
However, there are specific cases where a smaller numeral precedes a larger one, indicating subtraction. These are:
IbeforeV(4) orX(9)XbeforeL(40) orC(90)CbeforeD(400) orM(900)
Given a Roman numeral string, convert it to its corresponding integer value.
Example 1:
Input: s = "III"
Output: 3
Example 2:
Input: s = "LVIII"
Output: 58
Example 3:
Input: s = "MCMXCIV"
Output: 1994
Constraints:
- The input string
swill have a length between 1 and 15, inclusive. swill only contain the characters 'I', 'V', 'X', 'L', 'C', 'D', 'M'.- The input
sis guaranteed to represent a valid Roman numeral within the range [1, 3999].
Similar LeetCode problem: Roman to Integer
AI interview prep
Core dilemma
Efficiently parsing a string with context-dependent (subtractive) character values versus a simple additive approach.
2 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Roman to Integer
Roman to Integer
Roman numerals are represented by seven symbols: I (1), V (5), X (10), L (50), C (100), D (500), and M (1000).
Typically, numerals are written from largest to smallest, and their values are summed. For instance, XII is 10 + 1 + 1 = 12, and LVIII is 50 + 5 + 1 + 1 + 1 = 58.
However, there are specific cases where a smaller numeral precedes a larger one, indicating subtraction. These are:
IbeforeV(4) orX(9)XbeforeL(40) orC(90)CbeforeD(400) orM(900)
Given a Roman numeral string, convert it to its corresponding integer value.
Example 1:
Input: s = "III"
Output: 3
Example 2:
Input: s = "LVIII"
Output: 58
Example 3:
Input: s = "MCMXCIV"
Output: 1994
Constraints:
- The input string
swill have a length between 1 and 15, inclusive. swill only contain the characters 'I', 'V', 'X', 'L', 'C', 'D', 'M'.- The input
sis guaranteed to represent a valid Roman numeral within the range [1, 3999].
Similar LeetCode problem: Roman to Integer