7. Reverse Integer
Input: 123
Output: 321Input: -123
Output: -321Input: 120
Output: 21Last updated
Was this helpful?
Input: 123
Output: 321Input: -123
Output: -321Input: 120
Output: 21Last updated
Was this helpful?
Was this helpful?
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
#sign
s = cmp(x,0)
# reverse abs
r = int(`s*x`[::-1])
return s * r * (r < 2 ** 31)class Solution {
public int reverse(int x){
int result = 0;
while (x != 0)
{
int tail = x % 10;
int newResult = result * 10 + tail;
if ((newResult - tail) / 10 != result) return 0;
result = newResult;
x /= 10;
}
return result;
}
}