Skip to content

Commit 12e1d27

Browse files
author
nhimanshujain
committed
Added solution to Leetcode Question - 7 | Reverse Integer
1 parent 9391cdd commit 12e1d27

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

0007/reverse_integer.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
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
2+
def reverse(self, x):
3+
s = str(x)
4+
res = (int('-' + s[1:][::-1]) if s[0] == '-' else int(s[::-1]))
5+
return (res if -2147483648 <= res <= 2147483647 else 0)

0 commit comments

Comments
 (0)