[Leetcode]字符串转换整数 (ATOI)
文章目录
My solution:
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++的三目运算符
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论