题解 | #把字符串转换成整数(atoi)#
把字符串转换成整数(atoi)
https://www.nowcoder.com/practice/d11471c3bf2d40f38b66bb12785df47f
只需根据要求一步一步调试代码输出即可
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param s string字符串 # @return int整型 # class Solution: def StrToInt(self , s: str) -> int: # write code here s_new = s.strip() # 去除前导和后面的空格 # 判断相关的输入的符号 if s_new=='': return 0 index = 0 label_ = 1 if s_new[0]=='-': label_ = -1 index = index+1 if s_new[0]=='+': label_ = 1 index = index+1 res = 0 for i in s_new[index:]: if i<'0' or i>'9': break res = res*10 + int(i) return min(max(res*label_, -2**31), 2**31-1)