Skip to content

Instantly share code, notes, and snippets.

@mohneesh7
Created October 6, 2024 17:32
Show Gist options
  • Save mohneesh7/53fad36c4b90a1256a90d6271641da0f to your computer and use it in GitHub Desktop.
Save mohneesh7/53fad36c4b90a1256a90d6271641da0f to your computer and use it in GitHub Desktop.
Solution for Roman to Integer
# 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