8. 字符串转换整数 (atoi)

请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)。

函数 myAtoi(string s) 的算法如下:

读入字符串并丢弃无用的前导空格
检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。
读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。
将前面步骤读入的这些数字转换为整数(即,"123" -> 123, "0032" -> 32)。如果没有读入数字,则整数为 0 。必要时更改符号(从步骤 2 开始)。
如果整数数超过 32 位有符号整数范围 [−231,  231 − 1] ,需要截断这个整数,使其保持在这个范围内。具体来说,小于 −231 的整数应该被固定为 −231 ,大于 231 − 1 的整数应该被固定为 231 − 1 。
返回整数作为最终结果。
注意:

本题中的空白字符只包括空格字符 ' ' 。
除前导空格或数字后的其余字符串外,请勿忽略 任何其他字符。

用 int 来存结果,需要特判一下

不加注释

/**
 * <p>Title:  </p>
 * <p>Description: </p>
 *
 * @author zcm
 */
public class Solution {
    public int myAtoi(String s) {
        int k = 0;
        while(k < s.length() && s.charAt(k) == ' ') k++;
        if (k == s.length()) return 0;

        int symbol = 1;
        if (s.charAt(k) == '-') {
            symbol = -1;
            k++;
        } else if (s.charAt(k) == '+'){
            k++;
        }
        int res = 0;
        while (k < s.length() && s.charAt(k) >= '0' && s.charAt(k) <= '9'){
            // x 是一个正数
            int x = s.charAt(k) - '0';
            //MIN= -2147483648,MAX= 2147483647,循环里的 res 是一个绝对值。
            if (symbol > 0 && res > (Integer.MAX_VALUE - x) / 10) return Integer.MAX_VALUE;
           
            if (symbol < 0 && -res < (Integer.MIN_VALUE + x) / 10)
                return Integer.MIN_VALUE;
            
            if (-(res * 10 + x) == Integer.MIN_VALUE) return Integer.MIN_VALUE;
            res = res * 10 + x;
            k++;
            if (res > Integer.MAX_VALUE) break;
        }

        res *= symbol;
        return res;
    }
}

加注释

/**
 * <p>Title:  </p>
 * <p>Description: </p>
 *
 * @author zcm
 */
public class Solution {
    public int myAtoi(String s) {
        int k = 0;
        while(k < s.length() && s.charAt(k) == ' ') k++;
        if (k == s.length()) return 0;

        int symbol = 1;
        if (s.charAt(k) == '-') {
            symbol = -1;
            k++;
        } else if (s.charAt(k) == '+'){
            k++;
        }
        int res = 0;
        while (k < s.length() && s.charAt(k) >= '0' && s.charAt(k) <= '9'){
            // x 是一个正数
            int x = s.charAt(k) - '0';
            //MIN= -2147483648,MAX= 2147483647,循环里的 res 是一个绝对值。
            if (symbol > 0 && res > (Integer.MAX_VALUE - x) / 10) return Integer.MAX_VALUE;
            /**
             * 当输入 “-2147483647” 时,
             * 循环时,res 是这样产生的
             * 2
             * 21
             * 214
             * 2147
             * ...
             * 214748364,这时
             * 如果 if (symbol < 0 && -res <= (Integer.MIN_VALUE + x) / 10) 这样写,
             * 此时 (Integer.MIN_VALUE + x) / 10 == 214748364
             * 就会提前返回了。
             * 所以,必须 if (symbol < 0 && -res < (Integer.MIN_VALUE + x) / 10)
             */
            if (symbol < 0 && -res < (Integer.MIN_VALUE + x) / 10)
                return Integer.MIN_VALUE;
            //如果输入 “-2147483648” ,绝对值为 2147483648,比 Integer.MAX_VALUE 大,就会有溢出情况。
            // 在 res = res * 10 + x; 处可能发生溢出
            // 所以,需要单独特判
            if (-(res * 10 + x) == Integer.MIN_VALUE) return Integer.MIN_VALUE;
            res = res * 10 + x;
            k++;
            if (res > Integer.MAX_VALUE) break;
        }

        res *= symbol;
        return res;
    }
//    public static void main(String[] a){
//        System.out.println(new Solution().myAtoi("-2147483647"));
//    }
}

用 long 来存结果

/**
 * <p>Title:  </p>
 * <p>Description: </p>
 *
 * @author zcm
 * @date 2021/10/11 下午8:17
 */
public class Solution {
    public int myAtoi(String s) {
        int k = 0;
        while(k < s.length() && s.charAt(k) == ' ') k++;
        if (k == s.length()) return 0;

        int symbol = 1;
        if (s.charAt(k) == '-') {
            symbol = -1;
            k++;
        } else if (s.charAt(k) == '+'){
            k++;
        }
        long res = 0;
        while (k < s.length() && s.charAt(k) >= '0' && s.charAt(k) <= '9'){
            res = res * 10 + s.charAt(k) - '0';
            k++;
            if (res > Integer.MAX_VALUE) break;
        }

        res *= symbol;
        if (res > Integer.MAX_VALUE) res = Integer.MAX_VALUE;
        if (res < Integer.MIN_VALUE) res = Integer.MIN_VALUE;

        return Integer.parseInt(""+res);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可持续化发展

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值