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

    Update (2015-02-10):
    The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

    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这样一个函数,标准库中关于这个函数有详细介绍http://www.cplusplus.com/reference/cstdlib/atoi/?kw=atoi

    根据题目中的描述,我们需要做以下几件事情:

    1. 先跳过一开始的空格字符串
    2. 进行符号的判决,如果一开始读到的字符就是非法字符,直接返回0
    3. 开始读取数字,如果读到非法字符,直接输出当前数字,这个过程中注意处理溢出,如果超过最大值,返回INT_MAX,小于最小值,返回INT_MIN;
    4. 最后把基数与符号相乘,返回结果

    代码如下:(写的时候注意,有几个地方涉及到字符串的比较,比如 str[i] - '0' > 7 不要写成str[i] > 7)

     1 class Solution {
     2 public:
     3     int myAtoi(string str)
     4     { 
     5        int sign = 1, base = 0, i = 0, len = str.size();
     6         while (i < len && str[i] == ' ')
     7             i++;
     8         if(str[i] == '+' || str[i] == '-')
     9             sign = (str[i++] == '+' )? 1:-1;
    10         while (i < len && str[i] >= '0' && str[i] <= '9')
    11         {
    12             if (base > INT_MAX / 10 || (base == INT_MAX / 10 && str[i] - '0' > 7))
    13             {
    14                 if (sign == 1)
    15                     return INT_MAX;
    16                 else 
    17                     return INT_MIN;
    18             }
    19              base = base * 10 + (str[i++] - '0');   
    20         }
    21         return base * sign;
    22     }
    23 };
  • 相关阅读:
    【极角排序、扫描线】UVa 1606
    【计算几何】是时候知道这些函数了
    【技巧性(+递归运用)】UVa 1596
    【策略】UVa 11389
    【策略】UVa 1344
    夏季吃西瓜,会勤上厕所,会导致体内缺水,导致便秘,,会长豆豆
    Alt+Shift+R组合键,用来在一个java文件中批量的重命名变量。
    Pycharm-professional-2017.2.3破解安装
    通过反编译深入理解Java String及intern
    转:MyEclipse安装Eclipse Memory Analyzer插件,并进行错误文件分析流程
  • 原文地址:https://www.cnblogs.com/dapeng-bupt/p/8075345.html
Copyright © 2011-2022 走看看