LC8. 字符串转整数

public class Solution8 {
    public int myAtoi(String str) {
        if (str == null) return 0;
        str = str.trim();
        if (str.length() == 0) return 0;
        char c = str.charAt(0);
        boolean minus = false;
        if (c == '+')
            str = str.substring(1);
        else if (c == '-')
            minus = true;
        else if (c < '0' || c > '9')
            return 0;
        int ret = 0;
        for (int i = minus ? 1 : 0; i < str.length(); i++) {
            c = str.charAt(i);
            if (c < '0' || c > '9') break;
            if (!minus && (ret > Integer.MAX_VALUE / 10 || ret == Integer.MAX_VALUE / 10 && c > '7'))
                return Integer.MAX_VALUE;
            if (minus && (ret > -(Integer.MIN_VALUE / 10) || ret == -(Integer.MIN_VALUE / 10) && c > '8'))
                return Integer.MIN_VALUE;
            ret *= 10;
            ret += c - '0';
        }
        return minus ? -ret : ret;
    }
}
  • 注意溢出
发布了140 篇原创文章 · 获赞 2 · 访问量 1903

猜你喜欢

转载自blog.csdn.net/weixin_40602200/article/details/103946571