leetcode——8 String to Integer (atoi)(自定义字符串转整型,如何避开各种奇葩输入)

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.
Requirements for atoi:
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.

Hide Tags: Math String

解题思路:
题目不难,主要考察对各种输入的综合处理,如空字符串:“”; 多空格:“  123 1 2 1” ;多符号:“+-123” ;多字符:“+abc123”,以及溢出。
返回结果由两部分构成:基数+符号,因此需要将两部分分别求解。
在程序设计初就要针对各种输入进行处理。编程的逻辑思维大致分四步:
(1)空字符串的处理:如果字符串位空返回0即可
(2)空格的处理:使用循环遍历,将指针跳过空格即可
(3)符号的处理:设置一个符号标识sign,遍历时首先遇到的符号为输出结果的符号

(4)数字与溢出的处理。具体看代码

代码如下:


public static int myAtoi(String str)   
   {  
    int sign=1,base=0,index=0,digit=0;  
    //空字符串的处理  
    if (str.length()==0)  
    {  
        return 0;  
    }  
    //空格的处理  
    while (str.charAt(index)==' '&&index<str.length())  
    {  
        index++;  
    }  
    //确定符号  
    if (str.charAt(index)=='+'||str.charAt(index)=='-')  
    {  
        sign=str.charAt(index)=='+'?1:-1;  
        index++;  
        if (sign==1&&str.charAt(index)=='-')  
        {  
            return 0;  
        }  
        if (sign==-1&&str.charAt(index)=='+')  
        {  
            return 0;  
        }  
    }  
    //确定基数  
    for (int i = index; i < str.length(); i++)  
    {  
        char ch=str.charAt(i);  
        if (ch<'0'||ch>'9')  
        {  
            break;  
        }  
        digit = ch - '0';  
        if (ch>='0'&&ch<='9')  
        {  
            /* 
             * 溢出的处理 
             * (Integer.MAX_VALUE)/10<base,(Integer.MAX_VALUE)/10==base&&Integer.MAX_VALUE %10 <digit 
             * 分别表示两种会出现溢出的情况,特别是第二种2147483648时 
             */  
            if ((Integer.MAX_VALUE)/10<base||(Integer.MAX_VALUE)/10==base&&Integer.MAX_VALUE %10 <digit)  
            {  
                if (sign==1)  
                {  
                    return Integer.MAX_VALUE;  
                }  
                else  
                {  
                    return Integer.MIN_VALUE;  
                }  
            }  
            base=10*base+digit;  
        }  
    }  
    return base*sign;  
   }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值