Leetcode|Leetcode 12. Integer to Roman

Python 3 实现:
【Leetcode|Leetcode 12. Integer to Roman】源代码已上传 Github,持续更新。

""" 12. Integer to RomanGiven an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999. """class Solution: def intToRoman(self, num): """ :type num: int :rtype: str """integers = [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"] i = 0 result = ""while i < len(integers): if num >= integers[i]: result = result + romans[i] num = num - integers[i] i -= 1i += 1return resultif __name__ == '__main__':solution = Solution() print(solution.intToRoman(2))

    推荐阅读