public int Reverse(int x)
{
int result = 0, prevValue = 0;
while (x != 0)
{
int currDigit = x % 10;
result = result * 10 + currDigit;
// Overflow check
if ((result - currDigit) / 10 != prevValue)
{
return 0;
}
x /= 10;
prevValue = result;
}
return result;
}
No comments:
Post a Comment