Leetcode Day 3: Roman to Integer Explained

The problem is as follows:
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol: Value
I: 1
V: 5
X: 10
L: 50
C: 100
D: 500
M: 1000

For example, 2 is written as II in Roman numeral, just two ones added together. 12…


This content originally appeared on DEV Community and was authored by Simona Cancian

The problem is as follows:
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol: Value
I: 1
V: 5
X: 10
L: 50
C: 100
D: 500
M: 1000

For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer.

Here is how I solved it:

  • Let's make use of a dictionary to map each roman numeral to its integer value.
  • Initialize a variable result to 0, which we will use to store the final integer value.
roman_numbers = {'I' : 1, 'V' : 5, 'X' : 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000}      
result = 0
  • Iterate through the string.
  • If current character is greater than previous character, subtract twice the value of "s[char - 1]" and add the value of "s[char]". Here is where we handle the subtraction cases. For example, IV = 4 (I=1 is less than V=5, so add 5 and subtract 1 twice: 5 - 2 + 1.
  • Else, add the value of "s[char]" to the result. Here is where we handle the regular addition.
  • Return result, which is the integer value of the roman numeral.
for char in range(len(s)):
    if char > 0 and roman_numbers[s[char]] > roman_numbers[s[char - 1]]:             
        result += roman_numbers[s[char]] - 2 * roman_numbers[s[char - 1]]
    else:
        result += roman_numbers[s[char]]
return result

Here is the completed solution:

class Solution:
    def romanToInt(self, s: str) -> int:
        roman_numbers = {'I' : 1, 'V' : 5, 'X' : 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000}      
        result = 0

        for char in range(len(s)):            
            if char > 0 and roman_numbers[s[char]] > roman_numbers[s[char - 1]]:                
                result += roman_numbers[s[char]] - 2 * roman_numbers[s[char - 1]]
            else:               
                result += roman_numbers[s[char]]
        return result


This content originally appeared on DEV Community and was authored by Simona Cancian


Print Share Comment Cite Upload Translate Updates
APA

Simona Cancian | Sciencx (2024-07-04T04:38:26+00:00) Leetcode Day 3: Roman to Integer Explained. Retrieved from https://www.scien.cx/2024/07/04/leetcode-day-3-roman-to-integer-explained/

MLA
" » Leetcode Day 3: Roman to Integer Explained." Simona Cancian | Sciencx - Thursday July 4, 2024, https://www.scien.cx/2024/07/04/leetcode-day-3-roman-to-integer-explained/
HARVARD
Simona Cancian | Sciencx Thursday July 4, 2024 » Leetcode Day 3: Roman to Integer Explained., viewed ,<https://www.scien.cx/2024/07/04/leetcode-day-3-roman-to-integer-explained/>
VANCOUVER
Simona Cancian | Sciencx - » Leetcode Day 3: Roman to Integer Explained. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/04/leetcode-day-3-roman-to-integer-explained/
CHICAGO
" » Leetcode Day 3: Roman to Integer Explained." Simona Cancian | Sciencx - Accessed . https://www.scien.cx/2024/07/04/leetcode-day-3-roman-to-integer-explained/
IEEE
" » Leetcode Day 3: Roman to Integer Explained." Simona Cancian | Sciencx [Online]. Available: https://www.scien.cx/2024/07/04/leetcode-day-3-roman-to-integer-explained/. [Accessed: ]
rf:citation
» Leetcode Day 3: Roman to Integer Explained | Simona Cancian | Sciencx | https://www.scien.cx/2024/07/04/leetcode-day-3-roman-to-integer-explained/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.