您的位置:首页 > 其它

8 - leetcode Reverse Integer

2016-11-14 22:26 393 查看
注意越界问题…

#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
英文: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:
sign = 1
else:
x,sign = -x,-1
result = 0
while x:
result = result * 10 + x % 10
x = int(x/10
)
#Python语言特性...整数越界没发现,易错点...
if result < -2147483647 or result > 2147483647:
return 0

return result * sign

if __name__ == "__main__":
a = Solution()
print a.reverse(2147483647)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息