LeetCode-3 StringToInteger

要求:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

Subscribe to see which companies asked this question

做这道题总共花了三段时间,绝对花了5个小时以上了。
1. 第一次没看题目的以上介绍,直接写代码,失败是必然的。
2. 第二次用java的正则表达式还是总有一些用例出不来。
3. 第三次逐个字符判断,不断尝试才解决了所有的用例。
经过这些也明白了只有先动脑再动手才能在最短的时间里AC,毕竟不考虑所有情况只是瞎写只是浪费时间罢了。另外算法题尽量不要用java里已经现成的东西,最好像C语言一样从头开始,因为已经写好的东西可能并不适合题的情况。就像正则表达式在这道题里一样。
代码如下:

/**
 * @author lrx 2016年10月11日下午7:29:07
 */
public class MyAtoi2 {
    public static int myAtoi(String str) {
        double result = 0;
        char symbols = '+';
        str = str.trim();// 去空格
        if (str.length() == 0) {
            return 0;
        }
        char[] array = str.toCharArray();
        /*
         * if (array[0] == '-') {// 确定符号//若数组长度为0 则越界异常 symbols = '-'; }
         */
        /*
         * while (j < str.length() && (array[j] == '+' || array[j] == '-')) { if
         * (array[j] != symbols) { return 0; } j++; continue; }
         */
        int j = 0;
        if (array[j] == '+') {
            j++;
        } else if (array[j] == '-') {
            j++;
            symbols = '-';
        }
        for (int i = j; i < str.length(); i++) {
            char temp = array[i];

            // System.out.println(result);
            if (temp >= '0' && temp <= '9') {
                result = result * 10 + (temp - '0');
                // result = result * 10 + symbols*(temp - '0');//直接带上符号
                if ((symbols == '+') && result > Integer.MAX_VALUE) {// "-2147483648"出错,因为比较时没带符号,故上边给result加上符号
                    // return Integer.MAX_VALUE;
                    result = Integer.MAX_VALUE;
                }
                if ((symbols == '-') && result < Integer.MIN_VALUE) {
                    // return Integer.MIN_VALUE;使代码有统一出口
                    result = Integer.MIN_VALUE;
                }
                // System.out.println(result);
            } else {
                break;// 遇见非数字结束返回当前值//代码的另一个出口
            }
        }
        result = (symbols == '+') ? result : -result;
        // System.out.println(symbols);
        // return (symbols == '+') ? (int) result : -(int) result;
        return (int) result;
    }

    public static void main(String[] args) {

        // String str = "-123";忘加符号
        // String str = " -0018a42";
        // String str = "+";
        // String str = "-2147483648";
        String str = " --1";
        System.out.println(myAtoi(str));
    }
}// 9:13
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值