moonlightshadow123
6/19/2017 - 9:23 AM

12. Integer to Roman

  1. Integer to Roman
class Solution(object):
    def intToRoman(self, num):
        """
        :type num: int
        :rtype: str
        """
        values = [1000, 900, 500, 400,\
                       100, 90, 50, 40,\
                       10, 9, 5, 4,\
                       1]
        symbols = ['M', 'CM', 'D', 'CD',\
                        'C', 'XC', 'L', 'XL',\
                        'X', 'IX', 'V', 'IV',
                        'I']
        romanString = ''
        for i in range(len(values)):
            k = num / values[i]
            for _ in range(k):
                romanString += symbols[i] 
            num -= values[i] * k
        return romanString

https://leetcode.com/problems/integer-to-roman/#/description

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.