首页 > 其他 > 详细

LeetCode Medium:12. Integer to Roman

时间:2018-04-12 12:49:45      阅读:181      评论:0      收藏:0      [点我收藏+]

一、题目

Given an integer, convert it to a roman numeral.

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

 把给定的整数转换成罗马数字

二、思路

这道题其实跟13题是两个相反的过程,首先将罗马数字与整数用字典的形式存储起来,然后用给定的整数与之作比较处理。

三、代码

def intToRoman0(num):
    """
    :type num: int
    :rtype: str
    """

    IntToChar = {1000: "M",
                  900: "CM",
                  500: "D",
                  400: "CD",
                  100: "C",
                   90: "XC",
                   50: "L",
                   40: "XL",
                   10: "X",
                    9: "IX",
                    5: "V",
                    4: "IV",
                    1: "I",}
    string = ‘‘
    for i in IntToChar.keys():
        while num >= i:
            num -= i
            string+=IntToChar[i]

    print(string)
    return string

参考博客:https://blog.csdn.net/daigualu/article/details/73928733  https://blog.csdn.net/hcbbt/article/details/44026099

LeetCode Medium:12. Integer to Roman

原文:https://www.cnblogs.com/xiaodongsuibi/p/8806800.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!