原题为力扣的第8题
1、首先要去除字符串里的空格,这就需要我们拿到字符串的长度并且把字符串转化为char类型的数组,再利用一个下标遍历整个数组并且去除空格
2、接着是判断正负号,我们用sign来标记最后的整数是负号还是正号,也是用遍历数组找到正负号,正号sign为1,负号sign为-1
3、如果整个字符串都没有数字则返回0
4、最后就是注意数字溢出,新建一个放数字的变量res,边遍历数组边把获取到的数字移到res上
代码如下
public static int myAtoi(String str) {
int len = str.length();
char[] charArray = str.toCharArray();
// 1、去除前导空格
int index = 0;
while (index < len && charArray[index] == ' ') {
index++;
}
// 2、如果已经遍历完成(针对极端用例 " ")
if (index == len) {
return 0;
}
// 3、如果出现符号字符,仅第 1 个有效,并记录正负
int sign = 1;
char firstChar = charArray[index];
if (firstChar == '+') {
index++;
} else if (firstChar == '-') {
index++;
sign = -1;
}
// 4、将后续出现的数字字符进行转换
// 不能使用 long 类型,这是题目说的
int res = 0;
while (index < len) {
char currChar = charArray[index];
// 4.1 先判断不合法的情况
if (currChar > '9' || currChar < '0') {
break;
}
// 题目中说只能存储 32 位大小的有符号整数,下面两个if分别处理整数和负数的情况。
// 提前判断乘以10以后是否越界,但res*10可能会越界,所以这里使用Integer.MAX_VALUE/10,这样一定不会越界。
// 这是解决溢出问题的经典处理方式
if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10 && (currChar - '0') > Integer.MAX_VALUE % 10)) {
return Integer.MAX_VALUE;
}
if (res < Integer.MIN_VALUE / 10 || (res == Integer.MIN_VALUE / 10 && (currChar - '0') > -(Integer.MIN_VALUE % 10))) {
return Integer.MIN_VALUE;
}
// 合法的情况下,才考虑转换,每一步都把符号位乘进去
res = res * 10 + sign * (currChar - '0');
index++;
}
return res;
}