1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public: int reverse(int x) { int ret = 0; bool positive = true; if(x == INT_MIN) return 0; if(x < 0) { positive = false; x = -x; } while(x) { int num = x % 10; x /= 10; if(ret > INT_MAX / 10 || (ret == INT_MAX / 10 && num > INT_MAX % 10)) return 0; ret = ret * 10 + num; } return positive ? ret : -ret; } };
|