python creative method


  • 0
    M
    class Solution(object):
        def intToRoman(self, num):
            """
            :type num: int
            :rtype: str
            """
            table = {1: 'I', 5: 'V', 10: 'X', 50: 'L', 100: 'C', 500: 'D', 1000: 'M', 0: ''}
            count = {1:0, 5:0, 10:0, 50:0, 100:0, 500:0, 1000:0}
            num_list = (1000, 100, 10, 1)
            result = ''
            
            for index in range(len(num_list)):
                count = num / num_list[index]
                if count <= 3:
                    result += count * table[num_list[index]]
                if count == 4:
                    result += (table[num_list[index]] + table[5*num_list[index]])
                if count >= 5 and count < 9:
                    result += (table[5*num_list[index]] + (count-5)*table[num_list[index]])
                if count == 9:
                    result += (table[num_list[index]] + table[num_list[index-1]])
                num = num % num_list[index]
            return result
                
            
    

Log in to reply
 

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