https://leetcode-cn.com/problems/integer-to-roman/
代码:
# -*- coding:utf-8 -*- class Solution(object): def intToRoman(self, num): """ :type num: int :rtype: str """ num_list = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] alpha_list = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'] result = '' while num > 0: for i in range(len(num_list)): if num >= num_list[i]: num -= num_list[i] result += alpha_list[i] break return result pass if __name__ == '__main__': num = 20 print(Solution().intToRoman(num))
思路:这道题就是个简单的打表,值得一说的就是将罗马数字中特殊规则作为一般规则放入列表中,然后从大到小依次输出罗马数字字母。
0 条评论