• LeetCode 12. Integer to RomanLeetCode


    整数转罗马数字

    first submission
    import math
    class Solution:
        def __init__(self):
            self.roman={1:'I',5:'V',10:'X',50:'L',100:'C',500:'D',1000:'M'}
        def intToRoman(self, num):
            """
            :type num: int
            :rtype: str
            """
            s=''
            while num>0:
                # number of digits -1
                digits=int(math.log10(num))
                
                # most high digits
                e=(num//10**digits)
    
                # num surplus
                print(e,digits,end="")
                if e>0:
                    #e==9 or e==4
                    
                    num%=e*10**digits
                    print(':',s,num)
                    if e%5==4:
                        s+=self.roman[10**digits]
                        e+=1
    
                    if e%5==0:
                        s+=self.roman[e*10**digits]
                        e=0
                        digits=0
                    elif e>5:
                        s+=self.roman[5*10**digits]
                        e-=5
                        digits=0
    
                    if 0<e<=3:
                        if e>1:
                            s2=self.roman[1]*e
                            s+=s2
                        if digits:
                            s+=self.roman[10**(digits)]
    
            return s
    

    Wrong Answer:

    Input:
    1
    Output:
    ""
    Expected:
    "I"
    
    Input:
    20
    Output:
    "IIX"
    Expected:
    "XX"
    
    second submission
    import time
    
    import math
    class Solution:
        def __init__(self):
            self.roman={1:'I',5:'V',10:'X',50:'L',100:'C',500:'D',1000:'M'}
        def intToRoman(self, num):
            """
            :type num: int
            :rtype: str
            """
            s=''
            while num>0:
                # number of digits -1
                digits=int(math.log10(num))
                
                # most high digits
                e=(num//10**digits)
    
                # num surplus
                print(e,digits,end="")
                if e>0:
                    #e==9 or e==4
                    
                    num%=e*10**digits
                    print(':',s,num)
                    if e%5==4:
                        s+=self.roman[10**digits]
                        e+=1
    
                    if e%5==0:
                        s+=self.roman[e*10**digits]
                        e=0
                        digits=0
                    elif e>5:
                        s+=self.roman[5*10**digits]
                        e-=5
                        #digits=0
    
                    if 0<e<=3 and digits==0:
                            s2=self.roman[1]*e
                            s+=s2
                    
                    if digits:
    
                        s+=self.roman[10**(digits)]
                        if e>0:
                            e-=1
                            num+=e*10**digits
                   
            return s
    
    if __name__ == "__main__":
        
        data = [
            {
                "input":3,
                "output":"III", 
            },
            {
                "input":4,
                "output":"IV"
            },
            {
                "input":9,
                "output":"IX"
            },
            {
                "input":58,
                "output":"LVIII"
            },
            {
                "input":1994,
                "output":"MCMXCIV"
            },
            {
                "input":1,
                "output":"I"
            },
            {
                "input":20,
                "output":"XX"
            },
            {
                "input":60,
                "output":"LX"
            }
     
        ];
        for d in data:
            
            print(d['input'])
            
            # 计算运行时间
            start = time.perf_counter()
            result=Solution().intToRoman(d['input'])
            end = time.perf_counter()
            
            print(result)
            if result==d['output']:
                print("--- ok ---> spend time: ",end-start)
            else:
                print("--- error ---> spend time: ",end-start)
                break
            
            print()
        else:
            print("success")
    

    总结:只是想个大概,然后就去做,遇到问题再改,头脑里没有一个全局观。惭愧惭愧。

  • 相关阅读:
    Unity给力插件之MegaFiers
    序列化存档之备忘脚本
    09 Spring Cloud的集群保护框架Hystrix
    08 在Spring Cloud中使用Feign
    07 REST客户端
    06 RestTemplate负载均衡
    05 第一个Ribbon程序
    04 Ribbon介绍
    01 在IDEA的同一目录下创建多个项目
    03 Eureka集群的搭建
  • 原文地址:https://www.cnblogs.com/warcraft/p/9374445.html
Copyright © 2020-2023  润新知