Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Solution 1:
class Solution { public: int reverse(int x) { int result = 0; while(x != 0) { if( x > 0 && result > (INT_MAX - x % 10) / 10 || x < 0 && result < (INT_MIN - x % 10) / 10 ) return 0; result = result * 10 + x % 10; x = x / 10; } return result; } };
时间: 2024-11-06 23:01:25