-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7-Reverse.py
More file actions
65 lines (55 loc) · 1.23 KB
/
7-Reverse.py
File metadata and controls
65 lines (55 loc) · 1.23 KB
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
# Python3
class Solution:
def reverse(self, x:int) -> int:
flag = False
if x < 0:
x = - x
flag = True
res = 0
while x:
res = res * 10 + x % 10
x = x // 10
if res > 2**31 - 1:
res = 0
if flag:
res = -res
return res
def main():
test = Solution()
ans = test.reverse(-123)
print(ans)
if __name__ == "__main__":
main()
# -2^31 <= x <= 2^31 - 1
# C++ 检查是否溢出 时间复杂度O(log(x)),空间复杂度O(1)
class Solution {
public:
int reverse(int x) {
int rev = 0;
while (x != 0) {
int pop = x % 10;
x /= 10;
if (rev > INT_MAX/10 || (rev == INT_MAX/10 && pop > 7)) {
return 0;
}
if (rev > INT_MAX/10 || (rev == INT_MAX/10 && pop < -8)) {
return 0;
}
rev = rev * 10 + pop;
}
return rev;
}
};
# Java
public int reverse(int x) {
int ans = 0;
while (x != 0) {
if ((ans * 10) / 10 != ans) {
ans = 0;
break;
}
ans = ans * 10 + x % 10;
x = x / 10;
}
return ans;
}