[leetcode]8. String to Integer (atoi)

8. String to Integer (atoi)

 

Implement atoi which converts a string to an integer.

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.

Note:

  • Only the space character ' ' is considered as whitespace character.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.

Example 1:

Input: "42"
Output: 42

Example 2:

Input: "   -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
             Then take as many numerical digits as possible, which gets 42.

Example 3:

Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.

Example 4:

Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical 
             digit or a +/- sign. Therefore no valid conversion could be performed.

Example 5:

Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
             Thefore INT_MIN (−231) is returned.

 

说实话这一题感觉真的难。。恶心了我很久很久。。以前各种分类剪枝还是没剪对。。

试了N次终于对了

首先我们可以知道我们可以用字符去减0,得出字符串所对应的数字,然后分类的时候方便很多。

因为字符‘0’对应的ascall是48,于是我们就知道49-57是1-9,分类的时候用>48 && < 57就OK了

通过他的样例,我们可以首先找出特殊的几个字符很明显,0," ",+,-绝对是4个特殊的字符

于是我们就要开始讨论各种各样的情况,一不小心就列举少了

首先是空字符串,这个没什么好说,直接返回0

然后考虑只有一个字符的情况,为什么这么考虑呢。。因为我比较笨。。单独拿出来解决了。

只有一个字符的时候,是这个4个字符以及其他字符就返回0,是数字就返回数字,这个单独处理就行。

然后是多个字符了,也就是一个很长的字符串

我的整体思路如下,首先是检查空字符串、字符串长度为1的时候,特殊处理掉。

然后是全0、全空格 空格加0等情况,这个一扫描结束直接处理掉

然后就先找到+号或者减号,或者数字作为起始点,找到结束点,然后再把得到的数字在与Int的最大最小比较,得出结果就OK了

中间也有一些坑,例如0-1等等,还要细分。

具体分类可以看代码,代码这次写得很复杂,完全可以优化,不过今天有点累了,先不管了

代码如下:

class Solution { 
    //比较两个字符串转化为数字的大小
    public boolean check(String a,String b){
        for(int i = 0;i < 10;i++){
            int temp1 = a.charAt(i) - 48;
            int temp2 = b.charAt(i) - 48;

            if(temp1 > temp2)
                return true;

            if(temp1 < temp2)
                return false;

            if(temp1 == temp2)
                continue;
        }
        return true;
    }
    
    public int myAtoi(String str) {
        int answer;

        boolean start_flag = false;  //判断是否开始的标记
        boolean all_zero = true;     //判断是否全0
        boolean all_space = true;    //判断是否全空格
        boolean is_false = false;    //判断是否为负数

        int begin_index = -2;        //开始的坐标用-2
        int end_index = str.length();  //结束的坐标用数组长度
          
        if(str.length() == 0)       //空字符串直接返回0
             return 0;
        
        //字符串长度为1单独解决
        if(str.length() == 1){
            if(str.charAt(0) <= 48 || str.charAt(0) > 57)
                return 0;
        }

        for(int i = 0; i < str.length();i++){
            int temp = str.charAt(i) - 0;

            //如果没有开始
            if(start_flag == false){
                //不是+-空格数字直接返回0
                if((temp < 48 || temp > 57) && temp != 45 && temp != 43 && temp != 32)
                    return 0;

                //+号
                if(temp == 43){
                    start_flag = true;
                    all_space = false;
                    all_zero = false;
                    begin_index = -1;
                }

                //-号
                if(temp == 45){
                    start_flag = true;
                    is_false = true;
                    all_space = false;
                    all_zero = false;
                    begin_index = -1;
                }

                //数字
                if(temp > 48 && temp <= 57){
                    start_flag = true;
                    begin_index = i;
                    all_space = false;
                    all_zero = false;
                }
                
                //0,如果是0,就把begin_index设置为-3
                if(temp == 48){
                    start_flag = true;
                    begin_index = -3;
                    all_space = false;
                }
            }
            //如果开始了
            else{
                if(temp < 48 || temp > 57){
                    //由+号或者-号开始
                    if(begin_index == -1){
                        return 0;
                    }//由0开始
                    else if(begin_index == -3){
                        return 0;
                    }//由数字开始
                    else{
                        end_index = i;
                        break;
                    }
                }

                if(temp > 48 && temp <= 57){
                    //由+号或者减号开始
                    if(begin_index == -1)
                        begin_index = i;

                    //由0开始
                    if(begin_index == -3){
                        begin_index = i;
                        all_zero = false;
                    }
                }
            }
        }


        //全空格 返回0
        if(all_space)
            return 0;

        //全0 返回0
        if(all_zero)
            return 0;

        String answer_string = new String();
        answer_string = str.substring(begin_index,end_index);

        //判断正负,比较
        if(is_false){
            //长度大于10直接返回最小值
            if(answer_string.length() > 10)
                return -2147483648;
            //长度= 10去比较
            if(answer_string.length() == 10){
                if(check(answer_string,"2147483648") == true)
                    return -2147483648;
            }
            return -Integer.valueOf(answer_string).intValue();
        }else{
            if(answer_string.length() > 10)
                return 2147483647;
            if(answer_string.length() == 10){
                if(check(answer_string,"2147483647") == true)
                    return 2147483647;
            }
            return Integer.valueOf(answer_string).intValue();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值