Problem Description:
Reverse digits of an integer.
EXAMPLE1:X = 123, return 321
example2:x = -123, return-321
Enter an integer number and then output its reversal
It should be noted that the reversal of the int variable may be more than the limit of int, such as 12345678999 reversal, it would be overflow,leetcode to return 0 if this condition occurs.
The idea is to get the value of each digit of the input number from the last loop and put it in a new number. The type of the returned number is set to long, and if the bounds of int are exceeded, return 0.
classSolution { Public: LongReverseintx) {//Start Typing your/C + + solution below//Do not write int main () function intSign = x >0?1: -1; X=ABS (x); LongRET =0; while(x) {intdigit=x%Ten; RET=ret*Ten+Digit; X/=Ten; } if(ABS (ret) >Int_max)return 0; returnRET *Sign ; }};
Leetcode #7 revert integer problem