5/09/2020

[LeetCode] 13. Roman to Integer

Problem : https://leetcode.com/problems/roman-to-integer/

Time Complexity = O(len(s))
Space Complexity = O(1)
 
class Solution(object):
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """

        symbols = {'I': 1, 'IV': 4, 'V': 5, 'IX': 9, 'X': 10, \
                   'XL': 40, 'L': 50, 'XC': 90, 'C': 100, \
                   'CD': 400, 'D': 500, 'CM': 900, 'M': 1000}

        i = 0
        num = 0
        while i < len(s):
            if i + 1 < len(s) and s[i] + s[i+1] in symbols:
                num += symbols[s[i]+s[i+1]]
                i += 2
            else:
                num += symbols[s[i]]
                i += 1

        return num
 

No comments:

Post a Comment