13.Roman to Integer

    xiaoxiao2021-03-25  158

    给一个罗马数字,把它转化成整形数字

    给定数的范围是1-399

    class Solution(object):

        def romanToInt(self, s):

            """         :type s: str         :rtype: int         """

            dict = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}

            num = 0

            for i in range(len(s[:-1])):

                if dict[s[i]] <dict[s[i+1]]:

                    num -= dict[s[i]]

                else:

                    num += dict[s[i]]

            return num + dict[s[-1]]

    转载请注明原文地址: https://ju.6miu.com/read-9601.html

    最新回复(0)