leetcode7 Reverse Integer

 1 """
 2 Given a 32-bit signed integer, reverse digits of an integer.
 3 Example 1:
 4 Input: 123
 5 Output: 321
 6 Example 2:
 7 Input: -123
 8 Output: -321
 9 Example 3:
10 Input: 120
11 Output: 21
12 """
13 """
14 为了防止溢出,本题需要先转str。再转int
15 还要考虑正负
16 考虑超出范围 返回0
17 用到了切片str[::-1],逆序
18 """
19 class Solution:
20     def reverse(self, x):
21         """
22         :type x: int
23         :rtype: int
24         """
25         x = int(str(x)[::-1]) if x >= 0 else - int(str(-x)[::-1])
26         return x if x < 2147483648 and x >= -2147483648 else 0
原文地址:https://www.cnblogs.com/yawenw/p/12333005.html