• 【Leetcode】【Easy】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.

    写一个a to i 字符串转为整形的函数。

    注意考虑所有可能的输入情况。

    题目不设置具体的输入规范,你需要思考收集所有可能的输入。

    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.

    函数要求:

    函数首先应该丢弃字符串首字符前的空白格;

    判断数字的正负符号,并将后面跟着的数字符号转化为数值;

    在数字字符之后,可能还包含其他不相关字符,这些字符应该省去;

    如果在空白格后,第一个字符不是有效的数字字符,或者根本没有有效的数字字符存在,那么不需要进行转化,返回空;

    如果没有有效的字符可以进行转化,那么返回0;

    如果正确的数值超过了能表示的范围,那么返回INT_MAX或者INT_MIN。

    解题:

    对于一个输入,基本需要考虑4种情况:

    1、删去头部的空白格(空格);

    2、判断数字的正负;

    3、注意整形溢出;

    4、处理无效的输入。

    解题步骤:

    1、不管输入是char *还是 stl string,需要三个int变量,正负号sign,结果值base,操作序号i;

    2、判断输入是否有效;

    3、删去头部的空格;

    4、判断正负符号,用一些技巧使正时sign为1,负时sign为0;

    5、判断字符是否是数字,如果是:

      (1)在计算此数字之前,查看当前base是否已经越界,即是否能安全的计算当前数字;

        即 base < INT_MAX / 10; 或者 base == INT_MAX / 10 && 当前数字 <= INT_MAX % 10;

      (2)计算当前数字;

    6、返回代符号的base;

    代码借鉴自Leetcode上题后讨论

     1 class Solution {
     2 public:
     3     int atoi(const char *str) {
     4         int sign = 1, base = 0, i = 0;
     5 
     6         while (str[i] == ' ')
     7             i++;
     8         
     9         if (str[i] == '-' || str[i] == '+') {
    10             sign = 1 - 2 * (str[i++] == '-'); 
    11         }
    12         
    13         while (str[i] >= '0' && str[i] <= '9') {
    14             if (base >  INT_MAX / 10 || (base == INT_MAX / 10 && str[i] - '0' > 7)) {
    15                 if (sign == 1) 
    16                     return INT_MAX;
    17                 else 
    18                     return INT_MIN;
    19             }
    20             base = 10 * base + (str[i++] - '0');
    21         }
    22         
    23         return base * sign;
    24     }
    25 };
  • 相关阅读:
    是否需要转方向 ?
    资深技术 Leader 曹乐:如何成为技术大牛
    动画:面试如何轻松手写链表?
    如何学算法~
    回溯算法团灭排列/组合/子集问题
    有了这套模板,女朋友再也不用担心我刷不动 LeetCode 了
    二分查找法:在女朋友回家之前可以玩多少个游戏?
    mybatis框架——实战练习——第一个spring boot + mybatis项目——在第一个项目的基础上删除一条记录
    mybatis框架——实战练习——第一个spring boot + mybatis项目——在第一个项目的基础上使用@Select注解查询某条数据
    mybatis框架——实战练习——第一个spring boot + mybatis项目——在第一个项目的基础上添加一条记录
  • 原文地址:https://www.cnblogs.com/huxiao-tee/p/4225378.html
Copyright © 2020-2023  润新知