字符串转换整数 (atoi)python
时间: 2024-06-12 11:11:20 浏览: 163
leetcode296-daily-coding-problem:一天的计划让失业者远离
详细解释代码:class Solution:
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
import re
pattern = r"[\s]*[+-]?[\d]+"
match = re.match(pattern, str)
if match:
res = int(match.group(0))
if res > 2 ** 31 - 1:
res = 2 ** 31 -1
if res < - 2 ** 31:
res = - 2 ** 31
else:
res = 0
return res
阅读全文