首页 > 其他 > 详细

leetcode 13 -> Roman to Integer

时间:2019-03-10 20:55:33      阅读:165      评论:0      收藏:0      [点我收藏+]

 

class Solution(object):
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        lista={I:1,V:5,X:10,L:50,C:100,D:500,M:1000}
        Value_sum=0
        last_Value=0
        for i in range(len(s)):
            if (lista[s[i]] in [1,10,100]) and ( last_Value not in [1,10,100]):
                Value_sum=Value_sum+lista[s[i]]
            elif (last_Value in [1,10,100]) and (last_Value<lista[s[i]]):
                Value_sum=Value_sum+lista[s[i]]-2*last_Value
            else:
                Value_sum=Value_sum+lista[s[i]]
            last_Value=lista[s[i]]
        return Value_sum
        

 

leetcode 13 -> Roman to Integer

原文:https://www.cnblogs.com/sea-stream/p/10506705.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!