• leetcode


    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.

    spoilers alert... click to show requirements for atoi.

    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.

    这个题目主要是考察各种边界情况,首先得了解atoi函数的需求

    1,忽略字符串开头的空格,从第一个非空格的字符开始处理

    2,正负符号,注意判断正负符号之后的第一个字符是否为数字

    3,遇到非数字的字符或者'/0'结束

    4,溢出判断

    5,设置返回值为0时的有效位(是真正为0还是输入不合法返回的0),这个仅仅是为了思考的严密性,与AC没有什么关系

    个人思路:

    1,按照题目提示,考虑到各种特殊的输入即可,难度不大

    代码:

     1 #include<cctype>
     2 #include<climits>
     3 
     4 class Solution {
     5 public:
     6     int atoi(const char *str) {
     7         if (!str)
     8         {
     9             return 0;
    10         }
    11 
    12         int i = 0;
    13 
    14         while (str[i] == ' ') //过滤前面的空格
    15         {
    16             ++i;
    17         }
    18 
    19         if ((str[i] != '+' || !isdigit(str[i + 1])) && (str[i] != '-' || !isdigit(str[i + 1])) && !isdigit(str[i])) //过滤不合法的输入,第一个字符不是'+','-'或数字,或者第一个字符是'+','-',但第二个字符不是数字
    20         {
    21             return 0;
    22         }
    23 
    24         long long result = 0;
    25         long long max_int = INT_MAX;
    26         long long min_int = INT_MIN;
    27         int sign = 1;
    28 
    29         if (!isdigit(str[i])) //判断正负号
    30         {
    31             if (str[i] == '-')
    32             {
    33                 sign = -1;
    34             }
    35             ++i;
    36         }
    37 
    38         while (isdigit(str[i]))
    39         {
    40             result = result * 10 + (str[i] - '0');
    41             ++i;
    42         }
    43         result *= sign;
    44 
    45         if (result > max_int)
    46         {
    47             return max_int;
    48         }
    49         else if(result < min_int)
    50         {
    51             return min_int;
    52         }
    53         else
    54         {
    55             return result;
    56         }
    57     }
    58 };
    View Code

    上述代码,有两个地方自我感觉不太好,一个是不合法输入的返回值与真正的0值如何区分,这个可以通过一个设置一个类似于errorno的标识位来区分此次的输入是否有效;第二个是采用long long类型来进行int类型溢出判断,如果没有long long这个类型,如何判断溢出呢?

    可以这么判断(在循环开头判断):

    1,如果result是正数,比较result与INT_MAX / 10,如果result > INT_MAX / 10,则此次循环结束,必然溢出,如果result == INT_MAX / 10且(str[i] - '0') > INT_MAX % 10,则也溢出,其它情况则不溢出

    2,如果result是负数,类似于上面的判断方法,也可以判断是否溢出

    这么判断不依赖与long long类型

  • 相关阅读:
    CBV进阶(一)
    uva 11748 Rigging Elections
    uva 11573 Ocean Currents(bfs+优先队列)
    无向图的欧拉路
    poj 3254 Corn Fields
    hdu 1114
    hdu 2639 (第k小的01背包)
    uva 1347 tour
    uva 437 The Tower of Babylon
    uva 1025 A Spy in the Metro(动态规划)
  • 原文地址:https://www.cnblogs.com/laihaiteng/p/3962242.html
Copyright © 2020-2023  润新知