7. Title leetcode integer inverted

Title Description

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 store the 32-bit signed integer, its value is in the range [-2
31 2 31 is - 1] Please According to this hypothesis, if integer overflow after reverse it returns 0.

python code

class Solution:
    def reverse(self, x: int) -> int:
        abs_x = abs(x)
        str_x = str(abs_x)
        str_x2 = str_x[::-1]
        abs_x2 = int(str_x2)
        if x > 0:
            return abs_x2 if abs_x2 < 2**31-1 else 0
        else:
            return -abs_x2 if -abs_x2 > -2**31 else 0
Published 33 original articles · won praise 3 · Views 5530

Guess you like

Origin blog.csdn.net/weixin_42990464/article/details/104873833