We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 98b50d1 commit d419e2dCopy full SHA for d419e2d
07整数反转.py
@@ -0,0 +1,40 @@
1
+'''
2
+给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
3
+示例 1:
4
+输入: 123
5
+输出: 321
6
+ 示例 2:
7
+输入: -123
8
+输出: -321
9
+示例 3:
10
+输入: 120
11
+输出: 21
12
+注意:
13
+假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231, 231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。
14
+这题可以参照回文数的解法
15
16
+class Solution:
17
+ def reverse(self,x:int)->int:
18
+ if x<0:
19
+ flag = True
20
+ else:
21
+ flag = False
22
+ x = abs(x)
23
+ re = 0
24
+ while x//10 != 0:
25
+ re = re*10 + x%10
26
+ x //= 10
27
+ re = re*10 + x
28
+ if flag:
29
+ re = 0 - re
30
+ if re < -2 ** 31 or re > 2 ** 31 - 1:
31
+ return 0
32
+ return re
33
+
34
35
36
+if __name__ == '__main__':
37
+ s = Solution()
38
+ print(s.reverse(1534236469))
39
40
0 commit comments