注意越界问题…
#!/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)