8 Lines Short Python solution using MOD less conditions.


  • 3
    S
    class Solution(object):
        def intToRoman(self, num):
            CHAR,ret="IVXLCDM__",""
            for i in xrange(3,-1,-1):
                r,num=num/(10**i),num%10**i
                if r%5==4:ret+=CHAR[i*2]+CHAR[i*2+1+r/5]
                elif r:ret+=CHAR[i*2+1]*(r/5)+CHAR[i*2]*(r%5)
            return ret

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.