Gary Lai
  • Home
  • About
Sign in Subscribe

7. Reverse Integer

Last updated on  Mar 11, 2024

7. Reverse Integer

class Solution:
    def reverse(self, x: int) -> int:  
        res = 0
        sign = x > 0
        x = abs(x)
        while x > 0:
            res = res * 10 + x % 10
            x //= 10
        res = res if sign else -res
        return res if res.bit_length() < 32 else 0
Previous 271. Encode and Decode Strings
Next 1095. Find in Mountain Array
Gary Lai © 2025
  • Sign up
Powered by Ghost