[leetcode]Reverse Integer @ Python
原题地址:https://oj.leetcode.com/problems/reverse-integer/
题意:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
解题思路:翻转整数。
代码:
class Solution: # @return an integer def reverse(self, x): answer = 0 sign = 1 if x > 0 else -1 x = abs(x) while x > 0: answer = answer * 10 + x % 10 x /= 10 return sign*answer