Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created July 28, 2018 10:38
Show Gist options
  • Save luojiyin1987/5b087bd550e61d484290ea595c0abfd7 to your computer and use it in GitHub Desktop.
Save luojiyin1987/5b087bd550e61d484290ea595c0abfd7 to your computer and use it in GitHub Desktop.
Roman to Integer
class Solution:
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
sum = 0
last = None
numbers = {"M":1000, "D":500, "C":100, "L":50, "X":10, "V":5, "I":1}
for st in s:
if last and numbers[st] >last:
sum -= 2 * last
sum += numbers[st]
last = numbers[st]
return sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment