[Leetcode]#7 Reverse Integer

来源:互联网 发布:unity3d代码 编辑:程序博客网 时间:2024/06/06 07:13
//#7 Reverse Integer//8ms 100%class Solution {public:    int reverse(int x)     {        if(x == -2147483648) return 0;         bool positive(true);        if(x < 0)         {            positive = false;            x = abs(x);        }        int result(0);        while(x != 0)        {            if(result >= 214748365 || (result == 214748364 && x >= 8))            {                //cout << "result: " << result << ", x%10: " << x%10 << " overflow\n";                return 0;            }            result = result*10 + x%10;            x = x / 10;        }        if(!positive) result = result * -1;        return result;    }};
0 0
原创粉丝点击