Leetcode-Python 整数转罗马数字

class Solution:
    def intToRoman(self, num: int) -> str:
        string = str(num)
        result = ''
        length = len(string)
        for s in range(length):
            e_num = int(string[s])*(10**(length-s-1))
            if e_num < 4000 and e_num >= 1000:
                result += (int(e_num/1000)*'M')
            if e_num == 900:
                result += 'CM'
            if e_num < 900 and e_num >= 500:
                result += ('D'+(int(e_num/100)-5)*'C')
            if e_num == 400:
                result += 'CD'
            if e_num < 400 and e_num >= 100:
                result += (int(e_num/100)*'C')
            if e_num == 90:
                result += 'XC'
            if e_num < 90 and e_num >= 50:
                result += ('L'+(int(e_num/10)-5)*'X')
            if e_num == 40:
                result += 'XL'
            if e_num < 40 and e_num >= 10:
                result += (int(e_num/10)*'X')
            if e_num == 9:
                result += 'IX'
            if e_num < 9 and e_num >= 5:
                result += ('V'+((e_num-5)*'I'))
            if e_num == 4:
                result += 'IV'
            if e_num < 4 and e_num >= 1:
                result += e_num*'I'
        return result

github项目地址:https://github.com/JockWang/LeetCode-Python

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值