My solution:
python3">import re
class Solution:
def myAtoi(self, str: str) -> int:
nums = re.findall(r"^[+-]?\d+" ,str.strip())
if nums:
num = int(nums[0])
if num < 0:
return max(num,-2147483648)
else:
return min(num,2147483647)
else:
return 0
- 正则表达式学习:https://deerchao.net/tutorial...
-
r"^[+-]?\d+"
表达:在字符串开头、+或-出现零次以及上、数字0-9出现一次及以上 -
str.strip()
与str.relaces(' ','')
的区别:str.strip()
去掉头尾的空格,str.relaces(' ','')
去掉所有空格 - python不存在类似c++的三目运算符