Reverse Integer Medium 11.3K 12.5K Companies Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
"""
Reverse Integer
Medium
11.3K
12.5K
Companies
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
Example 1:
Input: x = 123
Output: 321
Example 2:
import math
def reve(x):
if x<=((2**31)-1) and x>=(-2**31):
temp=abs(x)
rev=0
while(temp>0):
rem=temp%10
rev=rev*10+rem
temp=temp//10
#print(rev)
if rev<=((2**31)-1) and rev>=(-2**31):
if(x<0):
return -rev
else:
return rev
else:
return 0
num=int(input("Enter a positive or negative to reverse:"))
print(reve(num))
Reverse Integer Medium 11.3K 12.5K Companies Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
Reviewed by Shubham Prajapati
on
August 03, 2023
Rating:
No comments:
If you have any doubt so you can comment me then i will 100% help you ,Through comment Chat