Reverse digits of an integer.
EXAMPLE1:X = 123, return 321
example2:x = -123, return-321
Click to show spoilers.
Has a thought about this?
Here is some good questions to ask before coding. Bonus points for if you have already thought through this!
If the last digit are 0, what should the output being? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer and then the reverse of 1000000003 overflows. How should handle such cases?
For the purpose of this problem, assume a your function returns 0 when the reversed integer overflows.
Update (2014-11-10):
Test cases had been added to test the overflow behavior.
Subscribe to see which companies asked this question
According to test instructions, it is necessary to consider the case at the end of 0, the overflow condition, the case of negative numbers, etc.
1 Public classSolution {2 Public intReverseintx) {3 BooleanIsnegative =false;4 if(x < 0) isnegative =true;5 LongAns = 0;6x =Math.Abs (x);7 while(X > 0){8Ans = (ans*10) + (x%10);9X/= 10;Ten } One if(Ans > Integer.max_value | | ans < integer.min_value)return0; A if(isnegative) ans =-ans; - return(int) ans; - } the}
7. Reverse Integer Java Solutions