|

楼主 |
发表于 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[0:1] not in "0123456789-":
return 0
else:
if str1[0:1] == "-":
str1 = str1[1:]
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
|
|