这个题目,核心点在于判断乘和加的时候是否会溢出,总体而言是通过Integer.MAX_VALUE/10 和 a == Integer.MAX_VALUE/10时,下一位数字是否大于7 是否溢出
Integer.MAX_VALUE = 2147483647
Integer.MIN_VALUE = -2147483648

不过今天的重点是,自己AC后看到评论区里有人用chatgpt来写了一个ans。后面试了一下,确实可以AC,而且代码比自己写的还要精简(有点点惭愧= =)

手工版

class Solution {

    boolean positive = true;

    public int myAtoi(String s) {
        char[] chs = s.toCharArray();
        if (chs.length == 0) return 0;

        // 1.去除前导空格
        // 2.获取数字的正负,如果读取到 ‘-’ 为负, 读取到‘+’或者数字为正,读取到其他字符则为0
        // 3.从第一个数字开始读取,一直读取到下一个非数字字符或者字符串结尾位置

        chs = removeBlank(chs);
        chs = getSymbol(chs);
        if (chs == null) return 0;
        return toInt(chs);
        
    }

    // 去除前导0
    char[] removeBlank(char[] s) {
        char[] chs;
        int index = 0;
        for (int i = 0; i < s.length; i++) {
            index = i;
            if (s[i] != ' ') break;
        }
        chs = new char[s.length - index];
        System.arraycopy(s,index,chs,0, s.length - index);
        return chs;
    }

    // 获取数字符号 0 表示返回0,1表示为正数, -1 表示为负数
    char[] getSymbol(char[] chs) {
        char c = chs[0];
        if (c != '+' && c != '-' && c < '0' && c > '9') {
           return null; 
        }
        char[] ch;
        if (c == '+' || c >= '0' && c <= '9') {
            positive = true;
            if (c == '+') {
                ch = new char[chs.length - 1];
                System.arraycopy(chs, 1, ch, 0, chs.length - 1);
                return ch;
            } else {
                return chs;
            }
        }
        if (c == '-') {
            positive = false;
            ch = new char[chs.length - 1];
            System.arraycopy(chs, 1, ch, 0, chs.length - 1);
            return ch;
        }

        return null;
    }

    int toInt(char[] chs) {
        int i = 0;
        for(char c : chs) {
            if (c > '9' || c < '0') break;
            // 如果已经达到了最大值,直接返回
            if (i == Integer.MAX_VALUE || i == Integer.MIN_VALUE) return i;
            
            if (positive){
                // 判断这次迭代是否会溢出
                if (i > (Integer.MAX_VALUE - (c - '0'))/ 10) {
                    return Integer.MAX_VALUE;
                }
            } else {
                if (i > Integer.MIN_VALUE/ 10 * -1) {
                    return Integer.MIN_VALUE;
                }
                // 判断负数这次迭代是否会溢出
                if (i == Integer.MIN_VALUE/10 * -1 ) {
                    if (c-'0' > 8) return Integer.MIN_VALUE;
                }
            }
            i = i * 10 + (c - '0');
        }
        return positive ? i : -i;
    }

}

可能是边看蜡笔小新边写题目有点影响思路[doge]

AI版本

class Solution {
    public int myAtoi(String s) {
	int n = s.length();
    	int i = 0;
    	int sign = 1;
    	int res = 0;
    	// 去掉前导空格
    	while (i < n && s.charAt(i) == ' ') {
        	i++;
    	}
    	// 处理正负号
    	if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) {
        	sign = (s.charAt(i++) == '-') ? -1 : 1;
    	}
    	// 处理数字
    	while (i < n && Character.isDigit(s.charAt(i))) {
        	int digit = s.charAt(i++) - '0';
        	// 检查是否溢出
        	if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10 && digit > 7)) {
            	return (sign == 1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
        }
            res = res * 10 + digit;
        }
        return res * sign;
    }
}

Q.E.D.


每一个平凡的日常都是连续的奇迹