Leetcode 8 String to Integer

题目描述:

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.

------------------------------------------------------------------------------------------------------------------------------------------

算法首先需要过滤掉字符串str的空白字符,然后考虑几种情况:

1. str = "  -0012a42"  return -12

2. str = "+-2" return 0

3. str = "  -a8702" return 0

4. str = "1" return 1

5. str = "" return 0

6. str = "  sjdkajkd"  return 0

首先取得第一个非空字符,判断是否为正负号或者数字,如果不是return 0,如果是正负号,记录正负号,如果是数字,暂存在result中;

然后遍历字符串,取得每个字符,判断:

1.如果是正负号,因为先前已有正负号,则return 0;

2.如果是非数字,则判断是否为第二个字符,如果是第二个字符,且第一个为数字,则跳出循环,否则return 0,如果不是第二个字符,说明前面可能有数字,则跳出;

3.如果是数字,则result = result * 10 + (c - 48),放入累加器;

4.循环结束后,判断结果是否溢出,并且按正负改变符号;

代码如下:

class Solution {
public:
    int myAtoi(string str) {
        string s = "";
        long result = 0;
        char sign = ' ';
        bool flag = false;
        int count = 0;
       //过滤掉空白字符
       int i;
       for(i = 0; i < str.length(); i++)
       {
           if(str[i] == ' ')
            continue;
           else 
            break;
       }
       s = str.substr(i);
       
       if(s.length() == 0)
        return 0;
       
       //取得第一个字符,判断数字、正负号或者非数字
       char c = s[0];
       if(c == '+' || c == '-')
       {
        sign = c;
        flag = true;
       }
       else if(c >= 48 && c <= 57)
        result = c - 48;
       else
        return 0;
        
       //从第二个字符开始遍历
       for(int i = 1; i < s.length(); i++)
       {
           char c = s[i];
           if(c == '+' || c == '-')     //如果又是正负号,结束
            return 0;
           if(c < 48 || c > 57)     //如果非数字
           {
               if(i == 1)
               {
                if(s[0] >= 48 && s[0] <= 57)     //如果第一位是数字,则跳出
                    break;
                else         //如果第一位也不是数字,则结束
                    return 0;
               }        
               else     //如果不是第一个数字,说明前面存在数字,则跳出
                break;
           }
           if(c >= 48 && c <= 57)       //如果是数字
           {
               count++;
               result = result * 10 + (c - 48);
               if(count >= 9)       //判断溢出和符号
               {
                   if(flag == true && sign == '-')
                    if(result * -1 < INT_MIN)
                        return INT_MIN;
                   if(flag == true && sign == '+')
                    if(result > INT_MAX)
                        return INT_MAX;
                   if(flag == false && result > INT_MAX)
                    return INT_MAX;
               }
           }
           
       }
       if(flag == true && sign == '-')
        return (result * -1);
       return result;
        
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值