[LeetCode]#7.Reverse Integer
1 min readMar 29, 2020
Environment: Python 3.7
Key technique: Slice, 32 bit number binary
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Analysis:
- Use slice to get reverse number
- if the number is negative, Multiply it *-1.
- Don’t let number exceed 32 bit number.
Solution:
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
Submitted result:
Lesson learn:
The video maximum damage or hp is 2147483647 due to the 32 bit limit.
Reference: