Leetcode 7 Reverse Integer Solution in c++ | Hindi Coding Community

0

 

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the s
igned 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:


Input: x = -123

Output: -321

Example 3:


Input: x = 120

Output: 21


class Solution
{
public:
int reverse(int x)
{
long long int x1=abs(x);
long long int ans=0;
while(x1>0)
{
long long int temp=x1%10;
ans=ans*10+temp;
x1/=10;
}
if(ans>-pow(2,31) && ans<pow(2,31)-1){if(x<0){return (-1)*ans;}else{return ans;}}
return 0;
}
};

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !