LeetCode Medium:12. Integer to Roman
Posted nunca
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于LeetCode Medium:12. Integer to Roman的主要内容,如果未能解决你的问题,请参考以下文章
python http://stackoverflow.com/questions/306313/pythons-is-operator-behaves-unexpectedly-with-integ