李万金 发表于 2020-8-7 18:25:55

求助idle能运行 提交Leedcode却出错

本帖最后由 李万金 于 2020-8-7 18:33 编辑

编写的代码在idle上能运行,提交到leedcode却报错,提示说:我强行int一个空字符串s2,但我在idle上调试,直接print(s2),有值且是预期结果。(leedcode上选的是python3).有大佬知道是什么原因吗?
https://leetcode-cn.com/problems/string-to-integer-atoi/         题目链接
完整代码(标红是出错的地方)
class Solution:
    def myAtoi(self, str1: str) -> int:
      str1 = str.strip(str1)
      s2 = ""
      if len(str1) == 0 or str1 not in "0123456789-":
            return 0
      else:
            if str1 == "-":
                str1 = str1
               
                for i in str1:
                  if i not in "0123456789":
                        break
                  else:
                        s2 = s2 + i
                s2 = "-" + s2            
                if int(s2) > (-2**31) and int(s2) < (2**31 - 1):
                  return int(s2)
                else:
                  return -2**31
            else:
               
                for i in str1:
                  if i not in "0123456789":
                        break
                  else:
                        s2 = s2 + i
               
                if int(s2) > (-2**31) and int(s2) < (2**31 - 1):
                  return int(s2)
                else:
                  return 2**31 - 1

李万金 发表于 2020-8-7 18:29:35

本帖最后由 李万金 于 2020-8-7 18:36 编辑

https://leetcode-cn.com/problems/string-to-integer-atoi/    题目链接
完整代码:
class Solution:
    def myAtoi(self, str1: str) -> int:
      str1 = str.strip(str1)
      s2 = ""
      if len(str1) == 0 or str1 not in "0123456789-":
            return 0
      else:
            if str1 == "-":
                str1 = str1
               
                for i in str1:
                  if i not in "0123456789":
                        break
                  else:
                        s2 = s2 + i
                s2 = "-" + s2            
                if int(s2) > (-2**31) and int(s2) < (2**31 - 1):
                  return int(s2)
                else:
                  return -2**31
            else:
               
                for i in str1:
                  if i not in "0123456789":
                        break
                  else:
                        s2 = s2 + i
               
                if int(s2) > (-2**31) and int(s2) < (2**31 - 1):
                  return int(s2)
                else:
                  return 2**31 - 1

zltzlt 发表于 2020-8-7 18:30:06

李万金 发表于 2020-8-7 18:29
哪个图啊,报错的提示已经有了呀,

哦,看到了

zltzlt 发表于 2020-8-7 18:46:05

李万金 发表于 2020-8-7 18:29
https://leetcode-cn.com/problems/string-to-integer-atoi/    题目链接
完整代码:
class Solution:


它报错时输入的是什么数据?

李万金 发表于 2020-8-7 18:55:19

zltzlt 发表于 2020-8-7 18:46
它报错时输入的是什么数据?

leedcode上没有提示,我在本机上输入了各种数据都没问题,我很困惑

zltzlt 发表于 2020-8-7 18:57:39

李万金 发表于 2020-8-7 18:55
leedcode上没有提示,我在本机上输入了各种数据都没问题,我很困惑

我测试了一下看到了,输入 '-' 会出错

李万金 发表于 2020-8-7 19:00:30

zltzlt 发表于 2020-8-7 18:57
我测试了一下看到了,输入 '-' 会出错

真相了,大佬厉害,我知道哪儿错了{:10_266:}
页: [1]
查看完整版本: 求助idle能运行 提交Leedcode却出错