Reverse Integer

这道题需要注意一些特殊情况:

1、如果数字的最后几位是0的话,那么结果的前几位不能是0

2、对于有的数字,比如1000000003, 翻转过来会出现越界的情况。对于这种情况要怎样处理应该注意。

抛出异常是一种解决方法,以下代码没有考虑第二种情况的处理。

    int reverse(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(x == 0)
            return 0;
        int result = 0, mask = 1;
        if(x < 0){
            mask = -1;
            x = 0 - x;
        }
        while(x > 0){
            result = result*10 + x%10;
            x /= 10;
        }
        return result*mask;
    }
原文地址:https://www.cnblogs.com/waruzhi/p/3331962.html