码迷,mamicode.com
首页 > 其他好文 > 详细

7. Reverse Integer (整数的溢出)

时间:2016-04-07 09:41:29      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

int reverse(int x) {
    if(x == INT_MIN){//-2147483648
        return 0;
    }
    
    bool pos = x>= 0? true:false;
    x = abs(x);
    int ret = 0;
    
    while(x>0){
        if((ret > 214748364) || (214748364==ret && x%10 >7)) return 0;
        ret = ret*10 +  x%10;
        x /= 10;
    }
    if(!pos) ret = 0-ret;
    return ret;
}

 

7. Reverse Integer (整数的溢出)

标签:

原文地址:http://www.cnblogs.com/qionglouyuyu/p/5362123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!