美文网首页
12.leetcode题目讲解(Python):整数转罗马数字

12.leetcode题目讲解(Python):整数转罗马数字

作者: 夏山闻汐 | 来源:发表于2018-09-01 14:20 被阅读80次

    题目:


    题目

    这道题给出两种解法 :

    一种解法是通过条件判断求解,代码如下:

    '''
    
    Created on:  Saturday, September 01, 2018
    @author: Jedi Liu
    
    '''
    
    
    class Solution:
        def intToRoman(self, num):
            """
            :type num: int
            :rtype: str
            """
            n = num
            roman = ''
    
            m = n // 1000
            if m > 0:
                n = n - m * 1000
                while m > 0:
                    m = m - 1
                    roman = roman + 'M'
    
            c = n // 100
            if c > 0:
                if c == 9:
                    n = n - 900
                    roman = roman + 'CM'
                elif c >= 5:
                    n = n - 500
                    roman = roman + 'D'
                    c = c - 5
                    n = n - c * 100
                    while c > 0:
                        c = c - 1
                        roman = roman + 'C'
                elif c == 4:
                    n = n - 400
                    roman = roman + 'CD'
                elif c > 0:
                    n = n - c * 100
                    while c > 0:
                        c = c - 1
                        roman = roman + 'C'
    
            x = n // 10
            if x > 0:
                if x == 9:
                    n = n - 90
                    roman = roman + 'XC'
                elif x >= 5:
                    n = n - 50
                    roman = roman + 'L'
                    x = x - 5
                    n = n - x * 10
                    while x > 0:
                        x = x - 1
                        roman = roman + 'X'
                elif x == 4:
                    n = n - 40
                    roman = roman + 'XL'
                elif x > 0:
                    n = n - x * 10
                    while x > 0:
                        x = x - 1
                        roman = roman + 'X'
    
            if n == 9:
                roman = roman + 'IX'
            elif n >= 5:
                n = n - 5
                roman = roman + 'V'
                while n > 0:
                    n = n - 1
                    roman = roman + 'I'
            elif n == 4:
                roman = roman + 'IV'
            else:
                while n > 0:
                    roman = roman + 'I'
                    n = n - 1
            return roman
    
    
    s = Solution()
    print(s.intToRoman(27))
    
    

    这种方法效率有点差,代码也不够简练,但容易想到。下面给出第二种解法,这种解法是利用罗马数字和阿拉伯数字的对应关系求解,代码如下:

    '''
    
    Created on:  Saturday, September 01, 2018
    @author: Jedi Liu
    
    '''
    
    class Solution1:
        def intToRoman(self, num):
            """
            :type num: int
            :rtype: str
            """
            n = num
            roman = ""
            div = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
            romans = [
                "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV",
                "I"
            ]
            while n != 0:
                for i in range(len(div)):
                    d = div[i]
                    times = n // d
                    if times != 0:
                        roman = roman + (times) * romans[i]
                        n = n - d * times
            return roman
    

    如果你有更好的实现方法,欢迎交流。
    ps:如果您有好的建议,欢迎交流 :-D,也欢迎访问我的个人博客:tundrazone.com

    相关文章

      网友评论

          本文标题:12.leetcode题目讲解(Python):整数转罗马数字

          本文链接:https://www.haomeiwen.com/subject/syquwftx.html