leetcode第12题

# coding=utf-8
# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
class Solution(object):
    def intToRoman(self, num):
        """
        :type num: int
        :rtype: str

        建个字典,肯定的
        """
        I_dict={0:'',1:'I',2:'II',3:'III',4:'IV',5:'V',6:'VI',7:'VII',8:'VIII',9:'IX'}
        X_dict={0:'',1:'X',2:'XX',3:'XXX',4:'XL',5:'L',6:'LX',7:'LXX',8:'LXXX',9:'XC'}
        C_dict={0:'',1:'C',2:'CC',3:'CCC',4:'CD',5:'D',6:'DC',7:'DCC',8:'DCCC',9:'CM'}
        M_dict={0:'',1:'M',2:'MM',3:'MMM'}
        def I2X(num_a):#num%10是余数,num/10是商
            return num_a%10,num_a/10
        if num<10:
            int_o,b=I2X(num)
            str_fin=I_dict[int_o]
        elif num<100:
            int_t, int_o = I2X(num)
            str_t=I_dict[int_t]
            str_o=X_dict[int_o]
            str_fin=str_o+str_t
        elif num<1000:
            int_o, int_t = I2X(num)
            print int_t, int_o
            int_t,int_h=I2X(int_t)
            print int_t,int_o
            str_h=C_dict[int_h]
            str_t=X_dict[int_t]
            str_o=I_dict[int_o]
            str_fin=str_h+str_t+str_o
        else:
            int_o, int_t = I2X(num)
            int_t, int_h = I2X(int_t)
            int_h, int_th = I2X(int_h)
            str_h=C_dict[int_h]
            str_t=X_dict[int_t]
            str_o=I_dict[int_o]
            str_th=M_dict[int_th]
            str_fin=str_th+str_h+str_t+str_o
        return str_fin
a=Solution()
print a.intToRoman(10)

很长,也很常规

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值