• LeetCode Medium:12. Integer to Roman


    一、题目

    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

    既然无论如何时间都会过去,为什么不选择做些有意义的事情呢
  • 相关阅读:
    高中数学运算能力训练题
    vue @click.native
    vue_qqmapdemo1
    vuxdemo1
    使用命令行打开vscode
    nextjs-demo
    material-ui里面的withStyles是什么?
    material(一)
    有趣的npx
    在macbookpro上开启ssh服务
  • 原文地址:https://www.cnblogs.com/xiaodongsuibi/p/8806800.html
Copyright © 2020-2023  润新知