力扣:12正数转罗马数字(python) 简单粗暴解决方法
生活随笔
收集整理的這篇文章主要介紹了
力扣:12正数转罗马数字(python) 简单粗暴解决方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
class Solution:def intToRoman(self, num: int) -> str:res = []if num >= 1000:res.append("M"*(num//1000))num -= (num//1000*1000)##################:處理百位if num >= 900:res.append("CM")num = num-900if 500 <= num < 900:res.append("D")num -= 500if 400 <= num < 500:res.append("CD")num -= 400if 100<= num < 400:res.append("C" * (num//100))num -= (num//100*100)################## 處理十位if 90 <= num < 100:res.append('XC')num -= 90if 50 <= num < 90:res.append("L")num -= 50if 40 <= num:res.append("XL")num -= 40if 10 <= num < 40:res.append("X" * (num//10))num -= (num//10*10)####################### 處理個位數if num == 9:res.append("IX")num -= 9if 5 < num < 9:res.append("V")num = num - 5if num == 5:res.append("V")num -= 5if num == 4:res.append("IV")num -= 4res.append("I"*(num))return ("".join(res))
總結
以上是生活随笔為你收集整理的力扣:12正数转罗马数字(python) 简单粗暴解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 力扣:15三数之和(python)
- 下一篇: 力扣:13罗马数字转整数(python)