Skip to content

Instantly share code, notes, and snippets.

@BharathKumarS
Created February 17, 2020 19:25
Show Gist options
  • Save BharathKumarS/6ed39f979a2e5141f7dde874503a08da to your computer and use it in GitHub Desktop.
Save BharathKumarS/6ed39f979a2e5141f7dde874503a08da to your computer and use it in GitHub Desktop.
Reverse an integer with -2**31 through 2**31 - 1 constraint
#Solution works only on LeetCode
class Solution:
def reverse(self, x: int) -> int:
if not x < 0:
y = int(str(x)[::-1])
else:
x = x * -1
y = int(str(x)[::-1]) * -1
if y <= -2**31 or y >= 2**31 - 1:
y = 0
return(y)
@BharathKumarS
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment