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?
Throw an exception? Good, but what if throwing an exception was not an option? You would then has to re-design the function (ie, add an extra parameter).
The main idea is to flip an integer, but the two points need to be considered, one is the end is 0. The other is that it will be out of bounds after flipping, and the C + + codec may be cumbersome to handle, but Python is easy. directly to the STR type for processing. and calls the built-in function reverse, and when it goes int it takes its own initiative to handle 0 of the first band. Very easy. However, my program does not deal with the problem of cross-border. But still AC
Class solution: # @return An integer def reverse (self, x): if x<0: Sign =-1 else: sign = 1
STRX=STR (ABS (x)) r = strx[::-1] return Sign*int (R)
"Leetcode" "Python puzzle" Reverse Integer