Skip to content

Commit d419e2d

Browse files
committed
合并有序数组
1 parent 98b50d1 commit d419e2d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

07整数反转.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)