您的位置:首页 > 其它

13. Roman to Integer

2016-07-26 19:34 323 查看

13. Roman to Integer

Leetcode link for this question

Discription:

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

Analyze:

Code 1:

class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
dic={'I':1,'II':2,'III':3,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000,'v':5*1000,'x':10*1000,'l':50*1000,'c':100*1000,'d':500*1000,'m':1000*1000}
li=s
ln=[]
for i in li:
if ln and dic[i]>ln[-1]:
ln[-1]=-1*ln[-1]
ln.append(dic[i])
return sum(ln)


Submission Result:

Status: Accepted

Runtime: 176 ms

Ranking: beats 68.85%
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: