leetcode编程记录1 #8 String to Integer (atoi)

今天是第一次记录在leetcode上的做题的解题过程。
先看第一题:

  1. 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.

解题分析:
首先要理解到题目要求我们实现的是一个能够将ASCII码中数字转换成数字的函数,这个函数就是在c库函数中常用的atoi函数,这个函数将字符串转换成整型数字的时候有四点要求需注意:

  1. 该函数自动忽略字符串开始的空格符和制表符,直至遇到正负号或数字则开始转换。
  2. 当遇到正负号或数字后转换开始,这之后如果遇到非数字的字符则结束转换。
  3. 如果字符串开始不是空格符,制表符,正负号或者数字则函数的返回值为0
  4. 如果字符串所表示的数字超出了int数据所能表示的范围则当数字上溢时返回INT_MAX,下溢出时则返回INT_MIN。

    根据这四个要求可以开始对字符串开始处理了,下列是我的c++代码,因为一开始没有考虑到一些情况所以到后来修改完成的代码有一些臃肿。

#include <string>
#include <limits.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
using namespace std;

class Solution
{
public:
    int myAtoi(string str)
    {
        int result = 0;
        string sresult;
        for(int i = 0; i < str.size(); i++)
        {
            if((str[i] == ' ' || str[i] == '\t') && sresult.size() == 0)
            {
                continue;
            }
            else
            {
                if ((str[i] == '+' || str[i] == '-') && sresult.size() == 0)
                {
                    sresult.push_back(str[i]);
                }
                else if(str[i] <= '9' && str[i] >= '0')
                {
                    sresult.push_back(str[i]);
                }
                else
                {
                    break;
                }
            }
        }
        const int bitsOfInt = ((int)log10(INT_MAX)) + 1;

        if(sresult.size() == 0)
        {
            return 0;
        }
        else if(sresult.size() == 1)
        {
            if(sresult[0] == '+' || sresult[0] == '-')
            {
                return 0;
            }
            else
            {
                return sresult[0] - '0';
            }
        }
        else
        {
            if(sresult[0] == '+' )
            {
                if(sresult.size() > bitsOfInt + 1)
                {
                    return INT_MAX;
                }
                for (int i = 1; i < sresult.size(); i++)
                {

                    result = result * 10 + sresult[i] - '0';
                    if(result > INT_MAX / 10 && sresult.size() == bitsOfInt + 1 && i == sresult.size() - 2)
                    {
                        return INT_MAX;
                    }
                    if(result == INT_MAX / 10 && sresult.size() == bitsOfInt + 1)
                    {
                        if(sresult[i + 1] - '0' > INT_MAX - INT_MAX / 10 * 10)
                        {
                            return INT_MAX;
                        }
                    }

                }
            }
            else if(sresult[0] == '-')
            {
                if(sresult.size() > bitsOfInt + 1)
                {
                    return INT_MIN;
                }
                for (int i = 1; i < sresult.size(); i++)
                {
                    result = result * 10 - sresult[i] + '0';
                    if(result < INT_MIN / 10 && sresult.size() == bitsOfInt + 1 && i == sresult.size() - 2)
                    {
                        return INT_MIN;
                    }
                    if(result == INT_MIN / 10 && sresult.size() == bitsOfInt + 1)
                    {
                        if('0' - sresult[i + 1] < INT_MIN - INT_MIN / 10 * 10)
                        {
                            return INT_MIN;
                        }
                    }
                }
            }
            else
            {
                if(sresult.size() > bitsOfInt)
                {
                    return INT_MAX;
                }
                for (int i = 0; i < sresult.size(); i++)
                {
                    result = result * 10 + sresult[i] - '0';
                    if(result > INT_MAX / 10 && sresult.size() == bitsOfInt && i == sresult.size() - 2)
                    {
                        return INT_MAX;
                    }
                    if(result == INT_MAX / 10 && sresult.size() == bitsOfInt)
                    {
                        if(sresult[i + 1] - '0' > INT_MAX - INT_MAX / 10 * 10)
                        {
                            return INT_MAX;
                        }
                    }
                }
            }
        }
        return result;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值