leetcood学习笔记-13

错误记录

class Solution:
    def romanToInt(self, s: str) -> int:
        d = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
        r=0
        
        for i in range(len(s)):
            if d[s[i]]<d[s[i+1]] and i<len(s)-1:
                r-=d[s(i)]
            else:
                r+=d[s(i)]
        return r

会报错:TypeError: 'str' object is not callable

s(i)应改为s[i]

且第7行and的前后应为:

if i<len(s)-1 and d[s[i]]<d[s[i+1]]  :**先判断前面的,若将d[s[i]]<d[s[i+1]]写在前面,会索引越界

posted @ 2019-03-09 18:06  oldby  阅读(145)  评论(0编辑  收藏  举报