[LeetCode][python3]0013. Roman to Integer

class Solution:
def romanToInt(self, s: str) -> int:
rom=["M","D","C","L","X","V","I"]
digit=[1000,500,100,50,10,5,1]
result=0

if s.find("CM")>=0 or s.find("CD")>=0:
result-=200
if s.find("XC")>=0 or s.find("XL")>=0:
result-=20
if s.find("IX")>=0 or s.find("IV")>=0:
result-=2
for c in s:
for r,d in zip(rom,digit):
if c==r:
result+=d
return result
N2I -2020.03.18
class Solution:
def romanToInt(self, s: str):
symbol={'I':1,'V':5,'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
output=[0]
for i in s:
if symbol[i]>output[-1]:
output[-1]=symbol[i]-output[-1]
else:
output.append(symbol[i])
return sum(output)
N2I -2020.03.18

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store