c++:
class Solution {
public:
int reverse(int x)
{
long n = 0; // 为long int 的缩写, 取值范围为:-2^31 ~ (2^31 -1)。
while (x)
{
n = n * 10 + x % 10;
x /= 10;
}
return n > INT_MAX || n < INT_MIN ? 0 : n;
} // INT_MAX 2^31 -1 ,INT_MIN -2^31
};
python:
反转字符串
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
x = str(x)
a = int(x[::-1]) if not x.startswith("-") else -int(x[1:][::-1])
return 0 if a < -2**31 or a > 2**31 -1 else a