[LeetCode #12] Integer to Roman
2016-10-01 21:45 amadis 阅读(119) 评论(0) 编辑 收藏 举报Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
1 class Solution(object): 2 def intToRoman(self, num): 3 """ 4 :type num: int 5 :rtype: str 6 """ 7 if num > 3999: 8 return "" 9 10 table = [["M", "MM", "MMM"], 11 ["C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"], 12 ["X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"], 13 ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"] 14 ] 15 16 digit = [] 17 for i in range(4): 18 digit.append((num / pow(10, 3 - i)) % 10) 19 20 ret = "" 21 for i, d in enumerate(digit): 22 if d != 0: 23 ret += table[i][d - 1] 24 25 return ret
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步