前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【每天一道编程系列-2018.2.22】(Ans)

【每天一道编程系列-2018.2.22】(Ans)

作者头像
yesr
发布2019-03-14 13:04:48
3090
发布2019-03-14 13:04:48
举报
文章被收录于专栏:leetcode_solutions

【题目描述】

  Implement atoi to convert a string to an integer.    Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.    Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front. 

【题目大意】

  实现一个atoi函数,将字符串转成整形    要点:考虑所有的输入情况。 

【解题思路】

  前导字符是+或-或者没有,接下来输入的是数字,数字不能整数能表示的最大或最小数。如果超过就返回对应的最小或者最小的值。 

【本题答案】

代码语言:javascript
复制
/**
 * @author yesr
 * @create 2018-02-22 下午10:21
 * @desc
 **/
public class Test0222 {
    public int atoi(String str) {

        if (str == null || str.length() == 0) {
//            throw new NumberFormatException("Invalid input string: " + str);
            return 0;
        }

        // 如果字符串以空格开始
        int start = 0; //从开始找第一个不是空格的数
        boolean positive = true; // 是否为正数默认为true

        if (str.charAt(start) == ' ') {
            while (str.charAt(start) == ' ') {
                start++;
                if (start >= str.length()) { // 输入的全是空格
//                    throw new NumberFormatException("Invalid input string: " + str);
                    return 0;
                }
            }
        }

        if (str.charAt(start) == '-') { // 第一个非空白字符中-
            positive = false;
            start++;
        } else if (str.charAt(start) == '+') {// 第一个非空白字符是+
            start++;
        } else if (str.charAt(start) >= '0' && str.charAt(start) <= '9') { // 第一个非空白字符是数字
            return cal(str, start, true);
        } else { // 其它情况就抛出异常
//            throw new NumberFormatException("Invalid input string: " + str);
            return 0;
        }


        if (start >= str.length()) { // 第一个非空白字符是+或者-但也是最后一个字符
//            throw new NumberFormatException("Invalid input string: " + str);
            return 0;
        }

        if (str.charAt(start) > '9' || str.charAt(start) < '0') { // +或者-后面接的不是数字
//            throw new NumberFormatException("Invalid input string: " + str);
            return 0;
        } else {
            return cal(str, start, positive);
        }
    }

    private int cal(String str, int start, boolean positive) {

        long result = 0;
        while (start < str.length() && str.charAt(start) >= '0' && str.charAt(start) <= '9') {
            result = result * 10 + (str.charAt(start) - '0');

            if (positive) { // 如果是正数
                if (result > Integer.MAX_VALUE) {
//                    throw new NumberFormatException("Invalid input string: " + str);
                    return Integer.MAX_VALUE;
                }

            } else {
                if (-result < Integer.MIN_VALUE) {
//                    throw new NumberFormatException("Invalid input string: " + str);
                    return Integer.MIN_VALUE;
                }
            }

            start++;
        }

        if (positive) {
            return (int) result;
        } else {
            return (int) -result;
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年02月22日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档