LintCode : 颠倒整数

LintCode : 颠倒整数

将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数)。


class Solution:
    # @param {int} n the integer to be reversed
    # @return {int} the reversed integer
    def reverseInteger(self, n):
        # Write your code here
        if n >= 0:
            ans = int(str(n)[::-1])
        if n < 0:
            ans = int(str(n)[1:][::-1]) * (-1)
        if ans > 2147483647 or ans < -2147483648:
            return 0
        return ans

你可能感兴趣的:(lintcode)