[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

 

posted @ 2014-06-09 13:35  南郭子綦  阅读(4799)  评论(0编辑  收藏  举报