简体   繁体   English

代码中的错误是什么? 这似乎在我的 IDE 中运行良好,但在在线终端上不起作用

[英]What is the error in code? This seems to run fine in my IDE but doesn't work on online terminal

I'm trying to solve the leetcode question 8 String to integer(atoi) .我正在尝试解决 leetcode 问题 8 String to integer(atoi) But I'm getting a Runtime Error.但是我遇到了运行时错误。

TypeError: 'str' object is not callable Line 9 in myAtoi (Solution.py) TypeError: 'str' object is not callable Line 9 in myAtoi (Solution.py)

I understand it's due to a string object getting called but can't seem to figure out what to fix.我知道这是由于字符串 object 被调用,但似乎无法弄清楚要修复什么。

class Solution:
    def myAtoi(self, str: str) -> int:
        string = str.strip()
        if string[0]=='-' or string[0]=='+' or string[0].isdigit():
            new = string[0]
            for i in range(1,len(string)):
                if string[i].isdigit():
                    new = new+string[i]
                    if i!=len(string)-1 and (string[i+1]==' ' or string(i+1).isalpha()):
                        break
            if(int(new)>=0):
                if((int(new) & 0x7fffffff)==int(new)):
                    return int(new)
                else:
                    return int(0x7fffffff)
            else:
                if((int(new) & -0x80000000)==int(-0x80000000)):
                    return int(new)
                else:
                    return int(-0x80000000)

Can anyone please tell me what to fix here?谁能告诉我要在这里解决什么?

You don't return anything in the first if and you have an error in string calling, you use () instead of [] , the correct code will be: if第一次没有返回任何内容,并且在字符串调用中出现错误,则使用()而不是[] ,正确的代码将是:

class Solution:
    def myAtoi(self, str: str) -> int:
        string = str.strip()
        if string[0]=='-' or string[0]=='+' or string[0].isdigit():
            new = string[0]
            for i in range(1,len(string)):
                if string[i].isdigit():
                    new = new+string[i]
                    if i!=len(string)-1 and (string[i+1]==' ' or string[i+1].isalpha()):
                        break
            if(int(new)>=0):
                if((int(new) & 0x7fffffff)==int(new)):
                    return int(new)
                else:
                    return int(0x7fffffff)
            else:
                if((int(new) & -0x80000000)==int(-0x80000000)):
                    return int(new)
                else:
                    return int(-0x80000000)
        else:
          return 0

print(Solution().myAtoi("abc"))
print(Solution().myAtoi("ab"))
print(Solution().myAtoi("a"))
print(Solution().myAtoi("abc d e fgh"))
print(Solution().myAtoi("452 abc d e fgh"))
print(Solution().myAtoi("abc d e fgh 452"))
print(Solution().myAtoi("   -42"))

For anyone who's wondering the final answer to this question, it is:对于任何想知道这个问题的最终答案的人,它是:

class Solution:
    def myAtoi(self, str: str) -> int:
        string = str.strip()
        if not string:
            return 0
        if not string.strip("+-"):
            return 0
        if string[0]=='-' or string[0]=='+' or string[0].isdigit():
            new = string[0]
            for i in range(1,len(string)):
                if string[i].isdigit() or string[i]=='.':
                    new = new+string[i]
                    if i!=len(string)-1 and (string[i+1]==' ' or string[i+1].isalpha()):
                        break
                elif string[i]=='-' or string[i]=='+':
                    break
                elif string[i].isalpha():
                    break
                else:
                    return 0
            new2 = new.strip("+-")
            if new2:
                if(int(float(new))>=0):
                    if((int(float(new)) & 0x7fffffff)==int(float(new))):
                        return int(float(new))
                    else:
                        return int(0x7fffffff)
                else:
                    if((int(float(new)) & -0x80000000)==int(-0x80000000)):
                        return int(float(new))
                    else:
                        return int(-0x80000000)
            else:
                return 0
        else:
            return 0

Is this the optimal solution?这是最佳解决方案吗? No. But is the best I could come up with.不,但这是我能想到的最好的。

Runtime: 44 ms, faster than 47.19% of Python3 online submissions for String to Integer (atoi).运行时间:44 毫秒,比 Python3 在线提交字符串到 Integer (atoi) 的速度快 47.19%。 Memory Usage: 13.9 MB, less than 5.95% of Python3 online submissions for String to Integer (atoi). Memory 使用量:13.9 MB,不到 Python3 在线提交字符串到 Integer (atoi) 的 5.95%。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 代码在 IDE 中有效,但在终端控制台中无效 - Code works in IDE but doesn't work in Terminal Console 我的代码有什么问题? 我可以在我的 vscode 上正常运行,但它不适用于 heroku,问题来自此代码 - What's wrong with my code? I can run this fine on my vscode but it won't work on heroku, the prob comes from this code glibc rand()不适用于python,但在在线编译器中可以正常工作 - glibc rand() doesn't work with python but works fine in online compiler 我在TextWrangler中运行Python并且它工作正常但在Mac终端上却没有 - I run Python in TextWrangler and it works fine but on Mac Terminal it doesn't SL4A在我的Android终端中不起作用 - SL4A don't work in terminal ide of my android 我的代码无法正常工作,因为我使用的是在线IDE或是否有错误? - Is my code not working because i'm using an online IDE or is there an error? 我的 python 不返回 3,4,5,6,7 似乎它不起作用 - my python doesn't return 3,4,5,6,7 it seems that it doesn't work timezone() 最近有什么变化吗? 我的一些过去可以正常工作的代码不再工作了 - Did timezone() get any changes recently? Some of my code that used to work fine doesn't work anymore 在我的PC上正常工作的Python代码在我的Raspberry Pi上不起作用 - Python code that works fine on my PC doesn't work on my Raspberry Pi 我的代码在每次运行时都不起作用 - My Code Doesn't work every time i run it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM