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 };
  • 相关阅读:
    HTTP Status 404
    The error occurred while setting parameters--索引 3 超出范围 sqlserver2008
    hessian不能注入dao的问题解决
    怎么 才能显示Eclipse中Console的全部内容
    java mvc web 项目web.xml头改错了,死活加载不上springMvc的jar
    如何理解andriod中的View和framelayout两个概念
    Type Project has no default.properties file! Edit the project properties to set one.
    shell获取目录下(包括子目录)所有文件名、路径、文件大小
    找出1小时内占用cpu最多的10个进程的shell脚本
    awk统计命令(求和、求平均、求最大值、求最小值)
  • 原文地址:https://www.cnblogs.com/dapeng-bupt/p/8075345.html
Copyright © 2011-2022 走看看