zoukankan      html  css  js  c++  java
  • 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. 

    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.

    实现字符串转整数的函数,需要注意的几点: 本文地址

    1、对于非法输入,函数输出0(除了溢出)

    2、处理输入NULL

    3、处理数字前面的空格

    4、处理数字前面的0

    5、处理溢出

    6、注意“+ 123”输入是非法的

    class Solution {
    public:
        int atoi(const char *str) {
            if(str == NULL)return 0;//0、处理NULL
            while(*str == ' ')str++;//1、过滤掉空格
            bool sign = true;
            if(*str == '-'){sign = false; str++;}//2、判断符号位
            else if(*str == '+')str++;
            while(*str == '0')str++;//3、过滤掉数字前面的0
            
            long long res = 0, lastRes = 0;
            while(*str != '' && *str >= '0' && *str <= '9')
            {
                lastRes = res;
                res = res*10 + (int)(*str - '0');
                if(res > INT_MAX)return sign == true ? INT_MAX : INT_MIN;//4、判断是否溢出
                str++;
            }
            
            return sign == true ? res : res*-1;
        }
    };

    上面是把res声明为longlong来判断溢出,亦可以如下操作

    class Solution {
    public:
        int atoi(const char *str) {
            if(str == NULL)return 0;//0、处理NULL
            while(*str == ' ')str++;//1、过滤掉空格
            bool sign = true;
            if(*str == '-'){sign = false; str++;}//2、判断符号位
            else if(*str == '+')str++;
            while(*str == '0')str++;//3、过滤掉数字前面的0
            
            int res = 0, lastRes = 0;
            int limit = INT_MAX / 10;
            while(*str != '' && *str >= '0' && *str <= '9')
            {
                if(limit < res)return sign == true ? INT_MAX : INT_MIN;//4、判断是否溢出(两个数相乘溢出后,溢出的结果不一定比两个乘数小)
                lastRes = res;
                res = res*10 + (int)(*str - '0');
                if(res < lastRes)return sign == true ? INT_MAX : INT_MIN;//4、判断是否溢出
                str++;
            }
            
            return sign == true ? res : res*-1;
        }
    };

    【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3681845.html

  • 相关阅读:
    Fragment使用具体解释
    2014百度之星第一题Energy Conversion
    HDU 2602 Bone Collector 0/1背包
    Angular 2 + 折腾记 :(7) 初步了解表单:模板驱动及数据驱动及脱坑要点
    《开源框架那点事儿25》:对框架模板引擎实现方式的改造实录
    ROS机器人程序设计(原书第2版)补充资料 (柒) 第七章 3D建模与仿真 urdf Gazebo V-Rep Webots Morse
    sql改写or 改成union不等价数据变多
    在GDAL中添加GDALRasterizeGeometriesBuf函数
    多时相地图瓦片简单设想
    记录一次使用VS2015编译错误的原因查找(boost+gdal)
  • 原文地址:https://www.cnblogs.com/TenosDoIt/p/3681845.html
Copyright © 2011-2022 走看看