Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2 to the 31, 2 to the 31 - 1], then return 0.
def reverse(self, x: int) -> int:
result = -int(str(x)[1:][::-1]) if x < 0 else int(str(x)[::-1])
if -2 ** 31 <= result <= (2 ** 31) - 1:
return result
return 0
λλ λ¬Έμμ΄ μ¬λΌμ΄μ±μ ν΅ν΄ ν΄κ²°νλ€.
def reverse(self, x: int) -> int:
rev = int(str(abs(x))[::-1])
return (-rev if x < 0 else rev) if rev.bit_length() < 32 else 0
νμ΄μ¬μ int
μλ bit_length()
λΌλ λΉνΈ κΈΈμ΄λ₯Ό 리ν΄ν΄μ£Όλ ν¨μκ° μμλ€!