Created
October 6, 2024 17:32
-
-
Save mohneesh7/53fad36c4b90a1256a90d6271641da0f to your computer and use it in GitHub Desktop.
Solution for Roman to Integer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Solution for Roman to Integer | |
class Solution: | |
def romanToInt(self, s: str) -> int: | |
total = 0 | |
num_dict = { | |
'I' : 1, | |
'V' : 5, | |
'X' : 10, | |
'L' : 50, | |
'C' : 100, | |
'D' : 500, | |
'M' : 1000 | |
} | |
for rom in range(len(s)-1): | |
if num_dict[s[rom]] < num_dict[s[rom + 1]]: | |
total -= num_dict[s[rom]] | |
else: | |
total += num_dict[s[rom]] | |
total += num_dict[s[-1]] | |
return total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment