LEETCODE 7.Reverse Integer

来源:互联网 发布:推荐淘宝店知乎2016 编辑:程序博客网 时间:2024/06/16 16:26

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321


大意:反转整数

因为负数带符号,所以分开处理

转换成字符串,然后切片反转(这样是不是不合理,用的内置函数是不是偏离了算法范畴--)


class Solution(object):    def reverse(self, x):        """        :type x: int        :rtype: int        """        if x >= 0:            return int(str(x)[: : -1])        else:            return 0 - int(str(x)[-1: 0: -1])

然后惊奇的是提交oj,发现leetcodeoj有int整形长度限制,超出范围excepted就为0. mark一下,加个条件判断就行了。--




0 0