LeetCode算法题之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.
解题思路:
将字符串转为整型数,这里涉及到多种情况:
1、字符串前面有一个或多个空格,要忽略掉。如” 25845”,此时应该输出25845;
2、数字中混杂正负号,正负号是可以要的,但是只能是一个,如”+23”、”-45”,此时应该输出23和-45,要是”+-345”这种形式则返回0;正负号后面一定是数字,否则返回0,如”+f23”;
3、数字中混杂其他字符,只取连续的并且是第一位出现的数字字符串作为最后的转换结果,如” 23g346”,此时应该返回23;如”hj3535”这种形式返回0;
4、空串返回0;
5、转换后的结果必须在机器能表示的范围内,如果小于最小的INT_MIN,返回INT_MIN,大于最大的INT_MAX则返回INT_MAX;

class Solution
{
public:
    int myAtoi(string str)
    {
        int len=str.size();
        double res=0;
        //空串返回0
        if(len==0)
            return 0;
        int flag = 1;
        int spaceNum=0;
        //排除几种返回0的情况
        for(int i=0; i<len; i++)
        {
            if(str[i]==' ')
            {
                //统计空格的数量,方便后面计算
                spaceNum++;
                continue;
            }
            if(str[i]=='+' || str[i]=='-')
            {
                if(str[i+1]<'0' || str[i+1]>'9')
                {
                    return 0;
                }
                else
                {
                    //说明该字符串能转换成数字,跳出循环接下来处理
                    break;
                }
            }
            else if(str[i]<'0' || str[i]>'9')
            {
                return 0;
            }
            else
            {
                //说明该字符串能转换成数字,跳出循环接下来处理
                break;
            }
        }
        vector<int> Inte;
        for(int i=spaceNum; i<len; i++)
        {
            //判断正负
            if(str[i]=='-')
            {
                flag=0;
                continue;
            }
            else if(str[i]=='+')
            {
                flag=1;
                continue;
            }
            else if(str[i]<'0' || str[i]>'9')
            {
                break;
            }
            else
            {
                //拿到待转换的数字字符串
                Inte.push_back(str[i]-'0');
            }
        }
        for(int j=0; j<(int)Inte.size(); j++)
        {
            res += Inte[j]*pow(10,Inte.size()-j-1);
        }
        if(!flag)
        {
            res = res*(-1);
            if(res<INT_MIN)
            {
                return INT_MIN ;
            }
            else
            {
                return (int)res;
            }
        }
        else
        {
            if(res>INT_MAX)
            {
                return INT_MAX;
            }
            else
            {
                return (int)res;
            }
        }
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值