leetcode-32位整数反转并检查溢出

int reverse(int x) {
    int x_re = 0;
    while(abs(x/10) > 0)
    {
        if(isOverflowInt(x_re, x%10))
            return 0;
        x_re = x_re*10 + x%10;
        x /= 10;
    }
    if(isOverflowInt(x_re, x))
        return 0;
    x_re = x_re *10 +x;
    return x_re;
}
//判断是否溢出的函数:溢出位true,未溢出为false
bool isOverflowInt(int x, int pop)
{
    bool is=false;
    if(x>0){
        is = INT_MAX/10 < x ? true : false;
        if((!is) && (INT_MAX/10 == x))
            is = pop > 7 ? true : false;
    }
    if(x <0){
        is = INT_MIN/10 > x ? true : false;
        if((!is) && (INT_MIN/10 == x))
            is = pop < -8 ? true : false;
    }
    return is;
}

猜你喜欢

转载自blog.csdn.net/mixiaoxinmiss/article/details/80969895
今日推荐