zoukankan      html  css  js  c++  java
  • LeetCode 8. String to Integer (atoi)

    String to Integer (atoi)

    比较正统的一道题,基石系列。

    第1次提交
    import time
    
    class Solution:
        def __init__(self):
            self.INT_MAX=2**31-1
            self.INT_MIN=-2**31
        def myAtoi(self, str):
            """
            :type str: str
            :rtype: int
            """
    
            integer=0
            # into integer 
            flag=False
            # sign 
            sign=1
    
            for c in str:
    
                # empty char
                if c == ' ':
                    continue
                # sign 
                if c == '+':
                    continue
                if c == '-':
                    #print(c,'flag',flag)
                    if not flag:
                        flag=True
                        sign=-1
                        continue
                    else:
                        break
                # numbers
                if 48<=ord(c)<(48+10):
                    #print(c,'number')
                    flag=True
                    integer=integer*10+ord(c)-48
                    continue
    
                #print(c,'exit',ord(c))
                # other char exit
                break
    
            integer=sign*integer
            if integer > self.INT_MAX:
                integer=self.INT_MAX
            elif integer < self.INT_MIN:
                integer=self.INT_MIN
    
            return integer
    
    if __name__ == "__main__":
        
        data = [
            {
                "input":"42",
                "output":42, 
            },
            {
                "input":"      -42",
                "output":-42, 
            },
            {
                "input":"4193 with words",
                "output":4193, 
            },
            {
                "input":"words and 987",
                "output":0, 
            },
            {
                "input":"-91283472332",
                "output":-2147483648, 
            }
    
        ];
        for d in data:
            
            print(d['input'])
            
            # 计算运行时间
            start = time.perf_counter()
            result=Solution().myAtoi(d['input'])
            end = time.perf_counter()
            
            print(result)
            if result==d['output']:
                print("--- ok ---",end="	")
            else:
                print("--- error ---",end="	")
            
            print(start-end)
    

    Wrong Answer:

    Input:
    "+-2"
    Output:
    -2
    Expected:
    0
    

    千呐,(呐喊脸),多么只管的正负2啊,还不等于-2啊,哼。

    很自信(随意)的更改了,添加flag=True

    # sign 
    if c == '+':
        flag=True
        continue
    

    随手提交一下又有一个相似好玩的
    Wrong Answer:

    Input:
    "-+1"
    Output:
    -1
    Expected:
    0
    

    好吧,多个字符就退出。一定要严谨(认真脸)

    第2次提交
    # sign 
    if c == '+':
        if not flag:
            flag=True
            continue
        else:
            break
    

    Wrong Answer:

    Input:
    "   +0 123"
    Output:
    123
    Expected:
    0
    

    这个真疏忽了,都得退出的,顺便小改一下代码

    第3次提交
    import time
    
    class Solution:
        def __init__(self):
            self.INT_MAX=2**31-1
            self.INT_MIN=-2**31
        def myAtoi(self, str):
            """
            :type str: str
            :rtype: int
            """
    
            integer=0
            # into integer 
            flag=False
            # sign 
            sign=1
    
            for c in str:
    
                # empty char
                if c == ' ':
                    if flag:
                        break
                    continue
                # sign 
                if c == '+':
                    if flag:
                        break
                    flag=True
                    continue
                if c == '-':
                    #print(c,'flag',flag)
                    if flag:
                        break
    
                    flag=True
                    sign=-1
                    continue
                # numbers
                if 48<=ord(c)<(48+10):
                    #print(c,'number')
                    flag=True
                    integer=integer*10+ord(c)-48
                    continue
    
                #print(c,'exit',ord(c))
                # other char exit
                break
    
            integer=sign*integer
            if integer > self.INT_MAX:
                integer=self.INT_MAX
            elif integer < self.INT_MIN:
                integer=self.INT_MIN
    
            return integer
    
    if __name__ == "__main__":
        
        data = [
            {
                "input":"42",
                "output":42, 
            },
            {
                "input":"      -42",
                "output":-42, 
            },
            {
                "input":"4193 with words",
                "output":4193, 
            },
            {
                "input":"words and 987",
                "output":0, 
            },
            {
                "input":"-91283472332",
                "output":-2147483648, 
            },
            {
                "input":"+-2",
                "output":0, 
            },
            {
                "input":"-+1",
                "output":0, 
            },
            {
                "input":"   +0 123",
                "output":0, 
            }
    
        ];
        for d in data:
            
            print(d['input'])
            
            # 计算运行时间
            start = time.perf_counter()
            result=Solution().myAtoi(d['input'])
            end = time.perf_counter()
            
            print(result)
            if result==d['output']:
                print("--- ok ---",end="	")
            else:
                print("--- error ---",end="	")
            
            print(start-end)
    
  • 相关阅读:
    git添加本地项目到git
    GitLab项目迁移到Gerrit
    flask一些资料
    openldap sshkey & 用户自定义属性
    openldap复制
    openldap主机访问控制(基于用户组)
    openldap主机访问控制(基于ip)
    openldap自定义schema
    openldap主机访问控制(基于hostname)
    openldap权限sudo
  • 原文地址:https://www.cnblogs.com/warcraft/p/9362307.html
Copyright © 2011-2022 走看看