-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.整数反转.py
75 lines (73 loc) · 1.15 KB
/
7.整数反转.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#
# @lc app=leetcode.cn id=7 lang=python3
#
# [7] 整数反转
#
# https://leetcode.cn/problems/reverse-integer/description/
#
# algorithms
# Medium (35.27%)
# Likes: 3516
# Dislikes: 0
# Total Accepted: 1M
# Total Submissions: 2.9M
# Testcase Example: '123'
#
# 给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。
#
# 如果反转后整数超过 32 位的有符号整数的范围 [−2^31, 2^31 − 1] ,就返回 0。
# 假设环境不允许存储 64 位整数(有符号或无符号)。
#
#
#
# 示例 1:
#
#
# 输入:x = 123
# 输出:321
#
#
# 示例 2:
#
#
# 输入:x = -123
# 输出:-321
#
#
# 示例 3:
#
#
# 输入:x = 120
# 输出:21
#
#
# 示例 4:
#
#
# 输入:x = 0
# 输出:0
#
#
#
#
# 提示:
#
#
# -2^31
#
#
#
# @lc code=start
class Solution:
def reverse(self, x: int) -> int:
a =2147483648
if x>=0:
num = int(str(x)[::-1])
if num < a:
return num
else:
num = -int(str(x)[1::][::-1])
if num >- a:
return num
return 0
# @lc code=end