Skip to content

Latest commit

 

History

History
46 lines (35 loc) · 1.03 KB

12.md

File metadata and controls

46 lines (35 loc) · 1.03 KB

Integer to Roman

Description

link


Solution

See Code


Code

Complexity T : O(n) M : O(n)

class Solution:
    def intToRoman(self, num):
        """
        :type num: int
        :rtype: str
        """
        M = ['', 'M', 'MM', 'MMM']
        C = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM']
        X = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC']
        I = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX']    
        return M[int(num/1000)] + C[int(num%1000/100)] + X[int(num%100/10)] + I[num%10]


class Solution:
    def intToRoman(self, num):
        """
        :type num: int
        :rtype: str
        """
        dict = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]
        nums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
        result = ""
        for letter, n in zip(dict, nums):
            result += letter * int(num / n)
            num %= n
        return result