zoukankan      html  css  js  c++  java
  • leetcode string to integer

    class Solution {
    public:
        int atoi(const char *str)
        {
            int total;         /* current total */
            char sign;           /* if '-', then negative, otherwise positive */
            while ( isspace(*str) )
                ++str;
            if (*str == '-' || *str == '+')
                sign = *str++;    /* skip sign */
    
            total = 0;
            while (isdigit(*str)) {
                if((INT_MAX-(*str-'0'))/10<total)
                {
                    total=sign=='-'?INT_MIN:INT_MAX;
                }
                else total = 10 * total + (*str - '0');     /* accumulate digit */
                str++;    /* get next char */
            }
            if (sign == '-')
            {
                total=-total;
            }
            return total;   /* return result, negated if necessary */       
        }
    };
    

    判断溢出还可以使用long long类型

  • 相关阅读:
    [ZJOI2010]count 数字计数
    小雄数
    简单筛法函数
    [Noip模拟题]lucky
    欧拉线筛
    Intern Day78
    CodeForces1360C
    CodeForces1373B
    Intern Day78
    Intern Day78
  • 原文地址:https://www.cnblogs.com/tgkx1054/p/3093104.html
Copyright © 2011-2022 走看看