• 8. String to Integer (atoi)


    def solution(s):
        s=s.strip()
        if(s):
            l=len(s)
            first_n=-1
            b_n=-1
            last_n=-1
            f_n=-1
            for i in range(l):
                if(s[i].isdigit()):
                    first_n=i
                    continue
                if(not s[i].isdigit()):
                    if(s[i]=="."):
                        f_n=1
                        continue
                    if(i==0):
                        if(s[i]=="-"):
                            b_n=i
                            continue
                        elif(s[i]=="+"):
                            b_n=i
                            continue
                        else:
                            return 0
                    else:
                        if(s[i]=="-"):
                            b_n=i
                            continue
                        elif(s[i]=="+"):
                            b_n=i
                            continue
                    last_n=i
                    break
            print(first_n,last_n,f_n,b_n)
            if(s[0]=="-"):
                if(first_n==-1):
                    return 0
                x=s[b_n:first_n+1]
            elif(s[0]=="+"):
                x=s[b_n:first_n+1]
            else:
                if(last_n==-1):
                    x=s[:]
                else:
                    x=s[0:last_n]
            if(b_n==0 and first_n==-1 and last_n==-1):
                return 0
            print(f_n)
            if(not f_n==-1):
                if(("-" in s) and (("+") in s)):
                    return 0
                print(x)
                x=float(x)
            else:
                x=int(x)
            if(x>2147483647):
                return 0
            elif(x<-2147483647):
                return 0
            else:
                return x
        else:
            return 0
        
    s="+-2"
    print("k",solution(s))
  • 相关阅读:
    调接口
    500 Internal Server Error
    htaccess跨域
    Header add Access-Control-Allow-Origin: *
    如何写jquery插件
    笔记
    互联网思维,谁的思维?
    超强js博客值得学习!!!
    学好js的步骤
    NOIP2000 进制转换
  • 原文地址:https://www.cnblogs.com/sea-stream/p/10503598.html
Copyright © 2020-2023  润新知