一、leetcode地址
https://leetcode.com/problems/string-to-integer-atoi/
二、问题描述
三、代码实现
语言:Python3
代码:
class Solution:
def myAtoi(self, str: str) -> int:
length = len(str);i = 0;res = 0;token = 0
while i < length and str[i] == ' ':
i += 1
if i < length and (str[i] == '-' or str[i] == '+'):
token = str[i] == '-'
i += 1
while i < length and '0' <= str[i] <= '9':
res = res*10 + int(str[i])
i += 1
print(res)
if res >= 1<<31:
res = 1<<31 if token else (1<<31)-1
return -res if token else res