LeetCode OJ: 12 Integer to Roman

来源:互联网 发布:房屋装修效果图软件 编辑:程序博客网 时间:2024/05/20 14:19
Total Accepted: 59069 Total Submissions: 155093 Difficulty: Medium







Given an integer, convert it to a roman numeral.

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

Submitted Code

Language:Python
class Solution(object):    def intToRoman(self, num):        """        :type num: int        :rtype: str        """        res = ''        res += 'M' * (num // 1000)                num %= 1000        if num // 100 == 9:            res += 'CM'        if num // 100 < 9 and num // 100 >= 5:            res += 'D' + 'C' * (num // 100 - 5)        if num // 100 == 4:            res += 'CD'        if num // 100 < 4:            res += 'C' * (num // 100)                    num %= 100        if num // 10 == 9:            res += 'XC'        if num // 10 < 9 and num // 10 >= 5:            res += 'L' + 'X' * (num // 10 - 5)        if num // 10 == 4:            res += 'XL'        if num // 10 < 4:            res += 'X' * (num // 10)                     num %= 10        if num == 9:            res += 'IX'        if num < 9 and num >= 5:            res += 'V' + 'I' * (num - 5)        if num == 4:            res += 'IV'        if num < 4:            res += 'I' * num                    return res        




0 0
原创粉丝点击