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 9391cdd commit 12e1d27Copy full SHA for 12e1d27
0007/reverse_integer.py
@@ -1,9 +1,5 @@
1
class Solution:
2
- def reverse(self, x: int) -> int:
3
- val = 0
4
- pro = 1 if x > 0 else -1
5
- x = abs(x)
6
- while x:
7
- val = val * 10 + x % 10
8
- x //= 10
9
- return val * pro if (val <= 2 ** 31 - 1 and val >= -2 ** 31 ) else 0
+ def reverse(self, x):
+ s = str(x)
+ res = (int('-' + s[1:][::-1]) if s[0] == '-' else int(s[::-1]))
+ return (res if -2147483648 <= res <= 2147483647 else 0)
0 commit comments