本文共 760 字,大约阅读时间需要 2 分钟。
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
这里说一下罗马数的简单规则,左边的数>右边的数时,两个数相加,左边的数<右边的数时,右边的数减去左边的数,如
"DCXXI" = 621
romanToint = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000} #罗马数与整数之间的转换关系
class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ romanToint = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000} xlength = len(s) results = 0 for a in range(1,xlength): if romanToint[s[a]]>romanToint[s[a-1]]: results = results-romanToint[s[a-1]] else: results = results+romanToint[s[a-1]] results = results+romanToint[s[-1]] return results
转载地址:http://lwhci.baihongyu.com/