Article Directory:
- topic
- A script and notes
- A script logic
topic:
Gives a 32-bit signed integer, you need this integer number on each inverted.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
Suppose we have an environment can only store a 32-bit signed integer, then the value range of [-231 231--1]. Please According to this hypothesis, if integer overflow after reverse it returns 0.
And a script Note: When using [: 24ms]
class Solution: define a class # DEF Reverse (Self, X: int) -> int: # define a method and parameter X1 = STR (X) shaping the # character variable into a variable IF " - " in X1: # determines whether the string with negative X2 = X1 [. 1 :] # if so, using the method of slicing the first character string is removed, creating a new string X3 = X2 [:: -. 1 ] # reverse text X4 = int (X3) # string into a variable integer variable IF -X4 <-2 ** 31 is : # the title means, if the value of the reversed overflow 0 returns the return (0) else: Return (- X4) # returns after inversion integer otherwise the else : X2 = X1 [:: -. 1 ] # When the string with no negative sign "-", direct reversal string, and generates a new variable X3 = int (x2) # new string variables into integer variable iF X3> 2 ** 31 is : # likewise determined whether the value obtained by inverting an overflow return (0) # if the overflow, returns 0 if the else : return (X3 ) # otherwise return value after reversal
Script logic:
- This script first step you need to whether a negative sign "-" to judge, if with a minus sign, the corresponding processing is done
- Step two: integer variable into a string variable
- The third step: reverse a string variable
- Step 4: after inversion into a string variable integer variable