solution by <hemani167>

• SIMPLE SOLUTION FOR REVERSE INTEGER

Intuition

Check integer overflow condition,However, if the number is large such that the reverse overflows, the output will be some garbage value. If we give the input as any large number say 1000000045, then the output is some garbage value like 1105032705 or any other garbage value.

Algorithm

1. determine sign of the x(input integer)
2. make x absolute
3. create a loop
4. set remainder = x % 10
5. set reverse = reverse * 10 + remainder
6. check integer overflow condition.compare value of remainder with reverse % 10 if equal goto step 7 else return 0
7. set x = x/10
8. return reverse with sign
9. exit

C++

``````class Solution {
public:
int reverse(int x) {
int rev=0, sum=0,count=0;
int sign = x<0? -1:1;
x=abs(x);
while(x>0)
{
int remainder = x % 10;
rev = rev*10 + remainder;
if((rev%10)!=remainder)
{
return 0;
}
x = x/10;
count++;
}
return (sign * rev);
}
};

``````

Complexity Analysis

• Time complexity : O(count)
as count contains number of digits in integer x.
• space complexity : O(count)
as counts equals to number of digits which is need to store.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.