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.

    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.

    题意:将字符串转换成整数,需要注意的几点如下:

    1)从字符串开头开始遍历,若开始为空格,则跳过,直到第一个非空格的字符,没有非空格字符,则返回0,;

    2)若第一个非空的字符为正负号,则最后返回的数考虑正负号;

    3)若接下来的字符不是数字,则返回0. 完全不考虑小数点和自然数的情况;

    4)继续遍历中,遇到数字则转换成整数保存下来,若再次遇到非数字型的字符,则返回当前保存的值;

    5)遍历过程要考虑值的返回,若是超过整型类型的范围,则返回边界值;

    参考了,zhouworld16 ,代码如下:

     1 class Solution {
     2 public:
     3     int atoi(const char *str) 
     4     {
     5         if(str==NULL)    return 0;
     6         long long res=0;
     7         int i=0;
     8         bool flag=true;
     9         while(str[i]==' '||str[i]=='0')
    10             i++;
    11         if(str[i]=='+')
    12             i++;
    13         if(str[i]=='-')
    14         {
    15             flag=false;
    16             i++;
    17         }
    18 
    19         int len=strlen(str);
    20         for(;i<len;++i)
    21         {
    22             if(str[i]>='0'&&str[i]<='9')
    23             {
    24                 res=res*10+(str[i]-'0');
    25                 if(res>INT_MAX)
    26                     return flag?INT_MAX:INT_MIN;
    27             }
    28             else
    29             {
    30                 return flag?res:(-1)*res;
    31             }
    32         }
    33         return flag?res:(-1)*res;          
    34     }
    35 };

    值得注意的是,res的类型应该是范围比int大的类型,因为当字符串为"2147483648"仅比INT_MAX大1时,由于res只能保存2147483647,此时会产生溢出。返回-2147483648。所以res的类型要扩展,不这样做的话,参考Grandyang的博客

  • 相关阅读:
    译文高效的JavaScript.
    JavaScript 全半角转换
    js表单验证
    Js事件大全
    Javascript下的urlencode编码解码方法decodeURIComponent()
    加速Javascript:DOM操作优化
    javascript验证日期的函数
    javascript里面的小数计算出现近似值的解决办法
    【busybox】busybox使用总结 01
    Assemble 汇编语言的种类
  • 原文地址:https://www.cnblogs.com/love-yh/p/7077106.html
Copyright © 2011-2022 走看看