[LeetCode]#7.Reverse Integer

Input: 123
Output: 321
Input: -123
Output: -321
Input: 120
Output: 21
  1. Use slice to get reverse number
  2. if the number is negative, Multiply it *-1.
  3. Don’t let number exceed 32 bit number.
class Solution:
def reverse(self, x):
if x>=0:
ans=int(str(x)[::-1])
else:
x=x *-1
ans=int(str(x)[::-1])*-1
if ans>2147483647 or ans<-2147483648:
ans=0
return ans

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.