|
发表于 2020-3-12 19:00:55
|
显示全部楼层
A:- from functools import reduce
- class Solution:
- def myAtoi(self, str: str) -> int:
- Atoi=False
- List=[]
- sign=True
-
- for i in str:
- if Atoi:
- if i.isdigit():
- List.append(int(i))
-
- else:
- break
-
- else:
- if i.isdigit():
- Atoi=True
- List.append(int(i))
-
- elif i=='-':
- sign=False
- Atoi=True
-
- elif i=='+':
- Atoi=True
-
- elif i==' ':
- pass
-
- else:
- return 0
-
-
- res=reduce(lambda x,y:x*10+y,List,0)
- return min(res,2147483647)if sign else max(-2147483648,-res)
复制代码1079 / 1079 个通过测试用例
状态:通过
执行用时:44 ms
内存消耗:13.4 MB
提交时间:0 分钟之前
B:- from re import compile
- exp = compile('^\s*[+-]?\d+')
- class Solution:
- def myAtoi(self, str: str) -> int:
- if(res := exp.search(str)):
- if (res := int(res.group())) < 0:
- return max(-2147483648, res)
-
- else:
- return min(2147483647, res)
- else:
- return 0
复制代码执行结果:
通过
显示详情
执行用时 :
36 ms
, 在所有 Python3 提交中击败了
78.97%
的用户
内存消耗 :
13.4 MB
, 在所有 Python3 提交中击败了
5.78%
的用户
PS:大小限制真的蛋疼 |
|