NC100 把字符串转换成整数(atoi)

牛客华为机试题库【题号 HJ开头】(重点看)
牛客在线编程算法篇【题号NC开头】
剑指offer【题号 JZ开头】
力扣

1)原题链接

2)已有题解

3)代码

package string;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import static java.lang.System.in;

/**
 * NC100 把字符串转换成整数(atoi)
 */
public class NC100StringToInt {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(in));
        String str = bf.readLine();
        bf.close();

        int res = StrToInt(str);
        System.out.println(res);
    }

    /**
     * NC100 把字符串转换成整数(atoi)
     */
    public static int StrToInt(String s) {
        //空串
        if (s.isEmpty()) {
            return 0;
        }

        int length = s.length();

        int index = 0;
        //去掉前导空格,如果有
        while (index < length) {
            if (s.charAt(index) == ' ') {
                index++;
            } else {
                break;
            }
        }
        //去掉空格就什么都没有了
        if (index == length) {
            return 0;
        }

        int operator = 1;
        //处理第一个符号是正负号的情况
        if (s.charAt(index) == '+')
            index++;
        else if (s.charAt(index) == '-') {
            index++;
            operator = -1;
        }

        //去掉符号就什么都没有了
        if (index == length) {
            return 0;
        }

        int res = 0;
        while (index < length) {
            char chr = s.charAt(index);
            //后续非法字符,截断
            if (chr < '0' || chr > '9') {
                break;
            }
            // 处理越界
            // 因为如果越界被显示导致记错错误但是不会报错,所以这里才往前推判断出界
            // 基于公式res = res * 10 + operator * (chr - '0');
            // Integer.MAX_VALUE 2147483647
            // Integer.MAX_VALUE / 10 214748364
            // Integer.MAX_VALUE % 10 7
            // 如上值就会理解为何要如此判断
            if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10 && (chr - '0') > Integer.MAX_VALUE % 10)) {
                return Integer.MAX_VALUE;
            }
            // Integer.MAX_VALUE -2147483648
            // Integer.MAX_VALUE / 10 -214748364
            // Integer.MAX_VALUE % 10 -8
            if (res < Integer.MIN_VALUE / 10 || (res == Integer.MIN_VALUE / 10 && (chr - '0') > -(Integer.MIN_VALUE % 10))) {
                return Integer.MIN_VALUE;
            }
            res = res * 10 + operator * (chr - '0');
            index++;
        }

        return res;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值