Skip to content

Instantly share code, notes, and snippets.

@mertbozkir
Last active June 5, 2021 21:10
Show Gist options
  • Save mertbozkir/94e7d950de5b774c1dd4d2879b66a1f5 to your computer and use it in GitHub Desktop.
Save mertbozkir/94e7d950de5b774c1dd4d2879b66a1f5 to your computer and use it in GitHub Desktop.
String approach to reverse integer problem
class Solution:
def reverse(self, x: int) -> int:
ex = str(x)
if ex[0] == "-":
ex2 = ex[1:]
print(ex2[::-1])
if not((-2)**31 < int(ex2[::-1]) < 2**31 - 1):
return 0
return ex[0] + str(int(ex2[::-1]))
else:
if not((-2)**31 < int(ex[::-1]) < 2**31 - 1):
return 0
return int(ex[::-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment