Reverse digits of an integer. Example1:x= 123,return321example2:x=-123,return-321Click to show spoilers. have your thought about This?Here is some good questions to ask before coding. Bonus points forYouifYou have already thought through This!If the integer' s 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 ThisProblem, assume that your function returns 0When the reversed integer overflows. Update (2014-11-10): Test cases had been added to Test the overflow behavior.
This problem examines the operation of the number, which should be considered if overflow integer processing and positive and negative number processing.
1 Public classSolution {2 Public intReverseintx) {3 intRes =0;4 BooleanIsnegative =false;5 6 if(x<0){7Isnegative =true;8x =Math.Abs (x);9 }Ten One while(x! = 0){ A intdigit = X%10; -x = X/10; - if(Res > (integer.max_value-digit)/10){ the return0; - } -res = res*10 +Digit; - } + - if(Isnegative = =true){ +res = 0-Res; A } at - returnRes; - - - } -}
Leetcode-reverse Integer