法一:
class Solution(object):
def myAtoi(self, str):
str = str.strip()
if not str:
return 0
if str[0] != '-' and str[0] != '+' and not str[0].isdigit():
return 0
fu = True if str[0] == '-' else False
zheng =True if str[0] == '+' else False
start = 1 if fu or zheng else 0
res = 0
for j in range(start,len(str)):
if str[j].isdigit():
res = res*10 + ord(str[j])-ord('0')
else:
break
res = res if not fu else -res
if res > 2**31-1:
return 2**31-1
if res < -2**31:
return -2**31
return res
注1:ord() 函数是 chr() 函数(对于8位的ASCII字符串)或 unichr() 函数(对于Unicode对象)的配对函数,它以一个字符(长度为1的字符串)作为参数,返回对应的 ASCII 数值,或者 Unicode 数值,如果所给的 Unicode 字符超出了你的 Python 定义范围,则会引发一个 TypeError 的异常。
注2:判断是否溢出部分可写为:
if res > (1<<31)-1:
return (1<<31)-1
if res < -(1<<31):
return -(1<<31)
return res
技巧:python中的左移和右移与其他C/C++等的定义和结果都是不一样的。
python中的定义:右移n位定义为除以pow(2,n),左移n位定义为乘以pow(2,n),而且没有溢出(移除的int会升级为long类型)
class Solution:
def myAtoi(self, str: str) -> int:
#数字开始的地方
i = 0
while i < len(str) and str[i] == ' ':
i += 1
#字符串全为空格,归0
if i == len(str):
return 0
pos = True
if str[i] == '-':
pos = False
i += 1
elif str[i] == '+':
i += 1
j = i
#字符串为空或者首字符不是数字,归0
if j == len(str) or not str[j].isdigit():
return 0
res = 0
while j < len(str) and str[j].isdigit():
res = res * 10 + ord(str[j]) - ord('0')
j += 1
if not pos:
res = -res
if res > 2**31 - 1 :return 2**31 - 1
elif res < -2**(31):return -2**(31)
else:return res
法二
要繁琐一些,注意判断不符合的情况
class Solution:
def myAtoi(self, str: str) -> int:
str = str.strip()
if not str:
return 0
fu = False
start = 0
if str[0] in ['+', '-']:
start = 1
if str[0] == '-':
fu = True
if len(str[start:]) == 0 or not str[start].isdecimal() : #特殊情况
return 0
end = len(str)
for i in range(start, len(str)):
if not str[i].isdecimal(): #不是数字,break
end = i
break
res = float(str[start:end])
res = int(res) if not fu else -int(res)
if res > 2**31 - 1:
return 2**31 - 1
if res < -2**31:
return -2**31
return res
#############2021.7.11java版本更新#############
参考甜阿姨的做法:
这题的做法大概是这样:
1去掉前导空格
2再是处理正负号
3识别数字,注意越界情况。
这道题目如果只是简单地字符串转整数的话,就是简单地ans = ans * 10 + digit。 但是注意这道题目可能会超过integer的最大表示! 也就是说会在某一步 ans * 10 + digit > Integer.MAX_VALUE
。 *10
和+ digit
都有可能越界,那么只要把这些都移到右边去就可以了。 ans > (Integer.MAX_VALUE - digit) / 10
就是越界。
不过我的忠告是,等真正工作以后,尽可能地调用jdk的方法,比如Character.isDigit。如果没有你想要的api,也要尽量使用guava,apache common等常见的utils包,尽量不要自己造轮子,一是这样减少出错的可能,二是比较无脑,保护脑细胞~
class Solution {
public static int myAtoi(String s) {
if (s.length()==0){return 0;}
boolean negative= false;
int start = 0;
//先处理空格
while (start < s.length() && s.substring(start,start+1).equals(" ")){
start++;
}
//去掉空格后到了末尾,直接返回0
if (start == s.length()){return 0;}
//遇到负号
if (s.substring(start, start+1).equals("-")){
negative = true;
start ++;
}else if (s.substring(start,start+1).equals("+")){
//遇到正号
start ++;
}else if(!Character.isDigit(s.charAt(start))){
//遇到其他符号,直接返回0
return 0;
}
int ans = 0;
while(start < s.length() && Character.isDigit(s.charAt(start))){
int digit = s.charAt(start) - '0';
if(ans > (Integer.MAX_VALUE - digit) / 10){
return negative ? Integer.MIN_VALUE : Integer.MAX_VALUE;
}
ans = ans * 10 + digit;
start++;
}
return negative ? -ans : ans;
}
}