13. Roman to Integer

来源:互联网 发布:怎么样提高淘宝转化率 编辑:程序博客网 时间:2024/06/03 13:20

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%

0 0